programming languages/C
배열 연습
book_lover
2024. 5. 23. 00:55
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
int number[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
number[i] = rand() % 100;
for (int i = 0; i < 10; i++)
printf("number = %d\n", number[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 25
int main(void) {
srand(time(NULL));
int score[SIZE];
int total=0;
int max=-999,min=999;
float avg=0;
for(int i=0;i<SIZE;i++)
score[i]=rand()%100+1;
for(int i=0; i<SIZE;i++){
printf("%d\t",score[i]);
if((i+1)%10==0) printf("\n");
}
for(int i=0;i<SIZE;i++){
total+=score[i];
}
avg=(float)total/SIZE;
for(int i=0;i<SIZE;i++){
if(score[i]>max)
max=score[i];
if(score[i]<min)
min=score[i];
}
printf("\n");
printf("총점: %d\n",total);
printf("평균: %3.2f\n",avg);
printf("최고점: %d\n",max);
printf("최저점: %d\n",min);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 20
int main(void) {
// 선택정렬
srand(time(NULL));
int score[SIZE];
int temp=0;
int most=0;
for(int i=0;i<SIZE;i++)
score[i]=rand()%100+1;
for(int i=0; i<SIZE;i++){
printf("%d\t",score[i]);
if((i+1)%10==0) printf("\n");
}
// 내림차순
for(int i=0; i <SIZE; i++){
most=i;
for(int j=i+1; j<SIZE;j++){
if(score[most] < score[j])most = j;
}
temp= score[i];
score[i]=score[most];
score[most]=temp;
}
printf("\n\n");
for(int i=0; i<SIZE;i++){
printf("%d\t",score[i]);
if((i+1)%10==0)printf("\n");
}
//오름차순
for(int i=0; i <SIZE; i++){
most=i;
for(int j=i+1; j<SIZE;j++){
if(score[most] > score[j])most = j;
}
temp= score[i];
score[i]=score[most];
score[most]=temp;
}
printf("\n\n");
for(int i=0; i<SIZE;i++){
printf("%d\t",score[i]);
if((i+1)%10==0)printf("\n");
}
return 0;
}