domingo, 3 de fevereiro de 2013

Gerando números aleatórios em vetor dinâmico


#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <conio2.h>
#define tam 50
int main(){
system ("title GERANDO NUMEROS ALEATORIOS EM VETOR DINAMICO");
int i, j, x, y = 0, ord, aux, vezes;
srand ( time ( NULL ) );
int *matrix; int VET [50] ;
matrix = ( int* ) malloc ( tam * sizeof ( int ) );//printf("\n");
textcolor(LIGHTRED);gotoxy(21,3);printf("GERANDO NUMEROS ALEATORIOS EM VETOR DINAMICO");
textcolor(YELLOW);gotoxy(24,5);printf("Programa desenvolvido por:");
textcolor(LIGHTCYAN);gotoxy(52,5);printf("Samuel Lima");
textcolor(LIGHTBLUE);gotoxy(34,7);printf("sa_sp10@hotmail.com");
textcolor(LIGHTRED);gotoxy(30,9);printf("ESTES SAO OS NUMEROS GERADOS");
textcolor(YELLOW);gotoxy(2,11);
for ( i = 1; i < tam; i++ ) {
matrix [i] = rand () % 10; Sleep(25);
VET [y] =  matrix [i];
y++;
printf(" %d ",matrix [i] );
}
Sleep(800);
textcolor(LIGHTRED);gotoxy(25,15);printf("MOSTRANDO EM ORDEM OS NUMEROS GERADOS ");
ord = 0;
while(ord == 0){
ord = 1;
for(x = 1; x <= y; x++){

if( VET [x]  > VET [(x + 1)]){
aux = VET [x];
VET [x] = VET [(x + 1)];
VET [(x + 1 )] = aux;
ord = 0;
}
}
}
textcolor(YELLOW);gotoxy(2,17);
for(int x = 1; x < y ; x++){
printf(" %d ", VET [x]);
}
textcolor(LIGHTGRAY);gotoxy(32,23);printf("PRESSIONE QUALQUER TECLA");
getche();
system("cls");
textcolor(LIGHTRED);gotoxy(21,3);printf("GERANDO NUMEROS ALEATORIOS EM VETOR DINAMICO");
textcolor(LIGHTBLUE);gotoxy(34,5);printf("sa_sp10@hotmail.com");
textcolor(LIGHTGREEN);gotoxy(29,7);printf("NUMEROS ORDENADOS E REPETIDOS");
textcolor(YELLOW);gotoxy(3,9);
for(x = 1; x < y ; x++){
printf(" %d ", VET [x]);
}
Sleep(800);
printf("\n");
for (x = 1; x < y; x++) {
// conta o numero de vezes que VET [x] aparece no vetor a partir da posição x
vezes = 1;
j = x + 1;
while (j < y)
if (VET[j] != VET[x])
j++;
else {
vezes++;
// se repete, excli
y--;
VET[j] = VET[y];
}
printf("\n\tO VALOR %d OCORRE %d VEZES", VET[x], vezes);
}
Sleep(800);
textcolor(LIGHTGREEN);gotoxy(37,12);printf("NUMEROS GERADOS SEM OS REPETIDOS");
textcolor(YELLOW);gotoxy(38,14);
for (x = 1; x < y; x++){
printf(" %d ", VET[x]);
}
textcolor(LIGHTRED);gotoxy(45,16);printf("\aO ROCCO AGRADECE");
getche();
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.