Below is a source code to show 10 data saved in an array. Write the source code and save it as "array1.c". After compilation, confirm by executing it.
#include <stdio.h> int main(void) { int i; int data[10]={2,3,0,9,12,8,9,-2,7,6}; for (i=0; i<10; i++) { printf("data[%d] = %d\n", i, data[i]); } return (0); }
Below is a source code to create an arithmetic progression, save it into an array, and show it. Write the source code and save it as "array2.c". After compilation, confirm by executing it.
#include <stdio.h> int main(void) { int i; int data[10]; for (i=0; i<10; i++) { data[i] = 3 * i + 10; } for (i=0; i<10; i++) { printf("data[%d] = %d\n", i, data[i]); } return (0); }
Write a below source code and save it as "array3.c". After compilation, confirm by executing it with different input.
#include <stdio.h> int main(void) { int i, n; double data[100]; printf("Input integer or decimal repeatedly\n"); printf("Exit by Ctrl-d\n"); n = 0; while (scanf("%lf", &data[n]) != EOF) { n++; } for (i=0; i<n; i++) { printf("data[%d] = %f\n", i, data[i]); } return (0); }
(Execution result) $ ./a.out Input integer or decimal repeatedly Exit by Ctrl-d 3.4 9.2 -2.54 3 2 ^d <- Exit data[0] = 3.400000 data[1] = 9.200000 data[2] = -2.540000 data[3] = 3.000000 data[4] = 2.000000
We want to know a distribution of scores (integer from 0 to 100). Write a below source code and save it as "array4.c". After compilation, confirm by executing it with different input.
Interval (x: score) |
---|
0 <= x < 10 |
10 <= x < 20 |
20 <= x < 30 |
30 <= x < 40 |
40 <= x < 50 |
50 <= x < 60 |
60 <= x < 70 |
70 <= x < 80 |
80 <= x < 90 |
90 <= x < 100 |
x = 100 |
#include <stdio.h> #include <stdlib.h> #define N 11 int main(void) { int i, n, data, num[N]; // Initialization of array for (i=0; i<N; i++) { num[i] = 0; } // Input data printf("Input scores repeatedly\n"); printf("Exit by Ctrl-d\n"); n = 0; while (scanf("%d", &data) != EOF) { if (data < 0 || data > 100) { printf("Input scores from 0 to 100\n"); exit(1); } else { num[data/10]++; n++; } } // Show the distribution for (i=0; i<N-1; i++) { printf("%3d <= x < %3d : %3d persons\n", i*10, (i+1)*10, num[i]); } printf(" x = 100 : %3d persons\n", num[N-1]); printf("--------------------------------\n"); printf("Total : %3d persons\n", n); return (0); }
(Execution result) $ $ ./a.out Input scores repeatedly Exit by Ctrl-d 43 36 84 65 39 81 ^d <- Exit 0 <= x < 10 : 0 persons 10 <= x < 20 : 0 persons 20 <= x < 30 : 0 persons 30 <= x < 40 : 2 persons 40 <= x < 50 : 1 persons 50 <= x < 60 : 0 persons 60 <= x < 70 : 1 persons 70 <= x < 80 : 0 persons 80 <= x < 90 : 2 persons 90 <= x < 100 : 0 persons x = 100 : 0 persons ------------------------------ Total : 6 persons
Below is a source code to show each element of 3x3 matrix. Write a below source code and save it as "array5.c".
#include <stdio.h> #define N 5 int main(void) { int i, j; int mat[N][N]={ {2,0,2,0,0},{0,1,0,0,0},{3,0,6,0,0},{0,4,0,1,0},{0,0,0,7,1}}; for (i=0; i<N; i++) { for (j=0; j<N; j++) { printf("%3d", mat[i][j]); } printf("\n"); } return (0); }
(Execution result) $ ./a.out 2 0 2 0 0 0 1 0 0 0 3 0 6 0 0 0 4 0 1 0 0 0 0 7 1
Assuming m x n matrices A and B, sum of A and B are defined as shown in below. Based on Exercise 5, program to calcuate sum of A and B and save it as "array6.c". Assume that both matrices A and B are defined in the source code.
(Execution result) $ ./a.out A= 2 0 2 0 0 0 1 0 0 0 3 0 6 0 0 0 4 0 1 0 0 0 0 7 1 B= 0 0 0 2 0 2 1 0 0 5 0 0 3 0 0 1 4 0 1 0 2 0 0 0 1 A+B= 2 0 2 2 0 2 2 0 0 5 3 0 9 0 0 1 8 0 2 0 2 0 0 7 2
Below is a source code to show each character of string. Write the source code and save it as "array7.c". After compilation, confirm by executing it.
#include <stdio.h> int main(void) { char name[] = "JERS-1/SAR"; int i; for(i=0; name[i] != '\0'; i++){ printf("%c\n", name[i]); } return (0); }
(Execution result) $ ./a.out J E R S - 1 / S A R
Below is a source code to show the length of string. Write the source code and save it as "array8.c". After compilation, confirm by executing it.
int main(void) { char name[] = "JERS-1/SAR"; int i, n=0; for(i=0; name[i] != '\0'; i++) n++; printf("n = %d\n", n); return (0); }
In addition to Exercise 8, we want to show calendar of specific three continuous months. Using for-loop, program to show below calendars and save the source code as "for9.c". Input data are (1) day of the week of the first month, and (2) number of days of each month (first, second and third). Be careful that each figure should be right-aligned.
#include <stdio.h> int main(void) { char name[3][20] = {"English", "Math", "Physics"}; int i; for(i=0; i<3; i++){ printf("%s\n", name[i]); } return (0); }
(Execution result) $ ./a.out English Math Physics $
We want to calculate mean and standard deviation of three subjects (english, math and physics) among students. Equations of mean (upper) and standard deviation (lower) are shown in below.
Using array and referring to execution result, program to meet above requirement and save the source code as "array10.c".
Caution: Calculation of root requires following steps.
(Execution result) $ ./a.out Input number of students 5 Student 1's English 83 Student 1's Math 65 Student 1's Physics 78 Student 2's English 92 Student 2's Math 89 Student 2's Physics 100 Student 3's English 56 Student 3's Math 67 Student 3's Physics 62 Student 4's English 42 Student 4's Math 65 Student 4's Physics 52 Student 5's English 81 Student 5's Math 63 Student 5's Physics 55 Mean of English = 70.8 Mean of Math = 69.8 Mean of Physics = 69.4 Std dev of English = 18.7 Std dev of Math = 9.7 Std dev of Physics = 17.7 $