By using array, we can handle multiple values by a single variable.
#include <stdio.h>
int main() {
int a_arr[3] = {1, 2, 3};
printf("a[0] = %d\n", a_arr[0]);
printf("a[1] = %d\n", a_arr[1]);
printf("a[2] = %d\n", a_arr[2]);
return 0;
}
int a_arr[3]
.{...}
, we can give specific values into array variable at ones.a_arr[0]
.#include <stdio.h>
int main() {
int a_arr[3];
a_arr[0] = 5;
a_arr[1] = 10;
a_arr[2] = 15;
printf("a[0] = %d\n", a_arr[0]);
printf("a[1] = %d\n", a_arr[1]);
printf("a[2] = %d\n", a_arr[2]);
return 0;
}
a_arr[1] = 15
.scanf
function, we can assign any values. e.g., in the case of int
,scanf("%d", &a_arr[0]);
scanf("%d", &a_arr[1]);
scanf("%d", &a_arr[2]);
double
type array variable. For input from keyboard, in the case of double
type,scanf("%lf", &a_arr[0]);
scanf("%lf", &a_arr[1]);
scanf("%lf", &a_arr[2]);
For
statement is used to repeat a similar process.#include <stdio.h>
int main() {
int a_arr[3];
for(int i = 0; i < 3; i++){
a_arr[i] = (i+1)*5;
printf("a[%d] = %d\n", i, a_arr[i]);
}
return 0;
}
for(int i = 0; i < 3; i++)
means For
statement and this condition of repeating.i = 0
is initial value of i
.i < 3
is condition of continuance. During i < 3
, the process is repeated.i++
is the change statement after one process is completed.i = 0
, the second repeating process is run by i = 1
, and the third repeating process is run by i = 2
. That is, the codefor(int i = 0; i < 3; i++){
a_arr[i] = (i+1)*5;
printf("a[%d] = %d\n", i, a_arr[i]);
}
a_arr[0] = (0+1)*5;
printf("a[%d] = %d\n", 0, a_arr[0]);
a_arr[1] = (1+1)*5;
printf("a[%d] = %d\n", 1, a_arr[1]);
a_arr[2] = (2+1)*5;
printf("a[%d] = %d\n", 2, a_arr[2]);
i < 3
to i < 5
, then, please check an warning message displayed. This reason is out of array range in the case of int a_arr[3]
.#Define
statement can improve readability of code.#include <stdio.h>
#define MAX_SIZE 3 // <- check here
int main() {
int a_arr[MAX_SIZE];
for(int i = 0; i < MAX_SIZE; i++){
a_arr[i] = (i+1)*5;
printf("a[%d] = %d\n", i, a_arr[i]);
}
return 0;
}
#define MAX_SIZE 3
represents assigning 3
as MAX_SIZE
.MAX_SIZE
.for
statement is same.#define
statement, we can make safety code, that is, avoiding access out of range#define ARR_MAX_SIZE 100
, and check result.#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 10
int main() {
int a_arr[MAX_SIZE];
// random number range
int min_n = 1;
int max_n = 100;
srand(time(0));
// substituing random numbers to array
for(int i = 0; i < MAX_SIZE; i++){
a_arr[i] = rand() % (max_n - min_n + 1) + min_n;
}
// show
for(int i = 0; i < MAX_SIZE; i++){
printf("a[%d] = %d\n", i, a_arr[i]);
}
return 0;
}
#include <stdlib.h>
, #include <time.h>
, and srand(time(0))
as preparation.rand() % (max_n - min_n + 1) + min_n;
is the code for generating random number from min_n
to max_n
(see previous lecture material).min_n = 1
and max_n = 100
, the range of random is 1 to 100.MAX_SIZE
, min_n
, and max_n
. After that, check result.#include <stdio.h>
#define MAX_SIZE 5
int main() {
int a_arr[MAX_SIZE] = {10, 20, 30, 40, 50};
int s = 0;
s = a_arr[0]+a_arr[1]+a_arr[2]+a_arr[3]+a_arr[4]+a_arr[5];
printf("Total number = %d", s);
return 0;
}
MAX_SIZE
is 100, then, s = a_arr[0]+a_arr[1]+a_arr[2]+ ... +a_arr[98]+a_arr[99]
;#include <stdio.h>
#define MAX_SIZE 5
int main() {
int a_arr[MAX_SIZE] = {10, 20, 30, 40, 50};
int s = 0;
s = s + a_arr[0]; // s = 0 + 10 = 10
s = s + a_arr[1]; // s = 10 + 20 = 30
s = s + a_arr[2]; // s = 30 + 30 = 60
s = s + a_arr[3]; // s = 60 + 40 = 100
s = s + a_arr[4]; // s = 100 + 50 = 150
printf("Total number = %d", s);
return 0;
}
MAX_SIZE
is 100, then, s = s + a_arr[0]; s = s + a_arr[1]; ... s = s + a_arr[99];
;#include <stdio.h>
#define MAX_SIZE 5
int main() {
int a_arr[MAX_SIZE] = {10, 20, 30, 40, 50};
int s = 0;
for(int i = 0; i < MAX_SIZE; i++){
s = s + a_arr[i];
}
printf("Total number = %d", s);
return 0;
}
For
statement, we can make the simple code for calculating sum total.For
statement.#include <stdio.h>
#define MAX_SIZE 5
int main() {
int a_arr[MAX_SIZE] = {10, 20, 30, 40, 50};
int counter = 0;
for(int i = 0; i < MAX_SIZE; i++){
// when greater than 20, counting up
if(a_arr[i] > 20){
counter = counter + 1;
}
}
printf("No. of over than 20 = %d", counter);
return 0;
}
if(a_arr[i] > 20){counter = counter + 1;}
and changing i
from 0 to MAX_SIZE
.countr = 0;
if(a_arr[0] > 20){counter = counter + 1;}
if(a_arr[1] > 20){counter = counter + 1;}
if(a_arr[2] > 20){counter = counter + 1;}
if(a_arr[3] > 20){counter = counter + 1;}
if(a_arr[4] > 20){counter = counter + 1;}
For
and If
statements is useful not only c programming but other many programming.MAX_SIZE
and the condition for counting. Check output result.#include <stdio.h>
#define MAX_SIZE 10
int main() {
int age_arr[MAX_SIZE] = {10, 20, 60, 20, 30, -200, 50, 92, 20, 31};
int err_flag = 0;
// age check. whether invalid value or not
for(int i = 0; i < MAX_SIZE; i++){
if(age_arr[i] < 0){
err_flag = 1;
}
}
// result
if(err_flag == 0){
printf("No problem.");
}else if(err_flag == 1){
printf("Warning: including invalid value!");
}
return 0;
}
err_flag = 1
. When not including invalid age, err_flag = 0
err_flag
value, whether output error message or not is decided.if(age_arr[i] < 0 || age_arr[i] > 100){...}
int
-type array of size 5. Input value into array by using scanf
and for
statement. Not use equal symbol for assignment like a[0] = 3
.int
-type array's element. After that calculate these sum total by using For
statement.double
type array of size 100. After that, substitute random number from 1 to 100 into array's element. Finally, calculate average value of the all array's elements.scanf
and substituting them to array. After that, when including "minus" or "over 100", output warning message.