" async="async"> ', { cookie_domain: 'auto', cookie_flags: 'max-age=0;domain=.tistory.com', cookie_expires: 7 * 24 * 60 * 60 // 7 days, in seconds }); 책 알려주는 남자 :: LAB WEEK#8 Array

LAB WEEK#8 Array

프로그래밍 2018. 11. 5. 20:39

LAB.W08(Array1)_1105.pptx


1.

#include <stdio.h>

#define SIZE 10


void main()

{

char an1;

int i, an2, arr[10]={0};

while (1) {

printf("Do you want to make a reservation?(y or n)");

scanf("%c", &an1);

if(an1 == 'y')

{

printf("---------------------------------\n");

printf("1 2 3 4 5 6 7 8 9 10\n");

printf("---------------------------------\n");

for (i=0; i<SIZE; i++)

printf("%d ", arr[i]);

printf("\n");

printf("Which seat do you want to take?");

scanf("%d", &an2);

fflush(stdin);

if(arr[an2-1]==0)

{

arr[an2-1]=1;

printf("Seat NO%d has reserved for you!\n", an2);

}

else if (arr[an2-1]==1)

printf("NO%d has already taken. You can choose another one!\n", an2);

}

else if (an1 == 'n')

break;

}


2.

#include <stdio.h>

#define SIZE 10


void main()

{

int x, i, arr[10]={0};

do {

printf("Which candidates do you want to vote?(END:-1): ");

scanf("%d", &x);

arr[x-1]++;

} while(x!=-1);

printf("No. Result\n");

for(i=0; i<SIZE; i++)

printf("%d %d\n", i+1, arr[i]);

}



3.

#include <stdio.h>


int array_equal(int a[], int b[], int size)

{

int i, sum=0;

for(i=0; i<5; i++)

{

if(a[i]==b[i])

sum+=0;

else

sum+=1;

}

if(sum==0) return 1;

else return 0;

}


int main(void) 

{

int result;

int a[5] = {1, 2, 3, 4, 5};

int b[5] = {1, 2, 3, 4, 6};


result = array_equal(a, b, 5);


printf("RESULT: %d\n", result);

return 0;

}



4.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10

void main()
{
srand((unsigned)time(NULL));
int i, max=0, temp, k, arr[SIZE]={0};
for(i=0; i<100; i++)
{
k=rand()%10;
arr[k]++;
}
for(i=0; i<SIZE; i++)
{
if(arr[i]>arr[max])
{
temp = max;
max = i;
i = max;
}
printf("%d  %d\n", i, arr[i]);
}
printf("==========\n");
printf("가장 많이 나온 번호는 %d 입니다.", max);
}


5.

#include <stdio.h>


void scalar_mult (int a[][3], int scalar)

{

int  i, j;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

printf("%2d ", a[i][j]*scalar);

printf("\n");

}

}


void transpose(int a[][3], int b[][3])

{

int i, j;

for(i=0; i<3; i++)

{

for(j=0; j<3; j++)

{

b[i][j] = a[j][i];

printf("%2d", b[i][j]);

}

printf("\n");

}

}


int main(void) 

{

int a[][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int b[][3]={0};

scalar_mult(a, 2);

printf("\n");

transpose(a, b);

return 0;

}



'프로그래밍' 카테고리의 다른 글

Assignment #51 ~ #60  (0) 2018.12.19
LAB WEEK#7 Function  (0) 2018.10.31
블로그 이미지

얼음꿀차

책을 한 번 읽긴 읽어야겠는데 막상 읽자니 뭘 읽을지 고민되는 당신을 위해 읽을만한 책들을 알려드립니다!

,