SOLENT UNIVERSITY
Fig53-Task4-GradeA
In figure 54, it gives another example how the programme stops when the user enter ‘y’ lowercase.
Fig54-Task4-GradeA
Assessment 5
Introduction of Arrays
38
, SOLENT UNIVERSITY
How it explained in Assessment 1, the variable can store a single value per time, Arrays instead can store
more values together, let’s see a variable like box with only one space available and arrays like a garage
where it can be storing all those boxes. The only limit for arrays that if the datatype is a float it needs to be
only that, it cannot be mixed with integer or different datatype.
Example:
#include <stdio.h>
int main(void) {
int i=0; // int i=0; is using to count how many positions of the array needs to print out.
int n[]={1,2,3,4,5}; //This the number that are stored in n[]; (Array). It can be defined numbers or space,
where the user input the values with limited space, like array[10]; (it can contain ten
values in this array).
while(i<5){ // Since the code known that n[] has five integers, the while loop is saying that ‘i’ is
less than 5.It doesn’t include five since the Arrays are starting always from position
zero (arrays[0]) and in this case is n[0], n[1], n[2], n[3], n[4], five space inside the
array.
printf("%d\t",n[i]); //The code will print out 1 2 3 4 5. If the code needs to be print a specific number,
like number 3, inside the [] will be 2. Example: printf(“%d”,n[2]);, two is the
position where the number 3 is.
i++;} //i++; will count till the condition remain true.
return 0;
}
As showed above, the array is structure with a datatype of values, that it can be identified by space, user will
give the values, or they will be given by the programmer. It is, also, using a loop with the limit gave by the
space, that it can be written as i<5 or i<=4. It is recommended to use an escape sequence to print the arrays,
or the print might be not clear to understand. The arrays is always followed by to square brackets and if equal
a specific value the must be contained inside curly brackets. Also, the loops needs to be part of the arrays, to
check and print the conditions till remain true.
Grade D
In Grade D, the program will be doing mathematic operations using the arrays. The array is with specific
values inside and the user will be able to modify, swap them or just have some calculation. The menu will be:
A – Display all numbers
39
, SOLENT UNIVERSITY
B – Calculate the mean and range
C – Swap two numbers
D – Replace a single number
E – Repopulate full array
#include <stdio.h>
void gradeD() {
char choice;
int n[] = {21, 17, 16, 20, 38, 31, 45, 28, 15};
int i=0;
float sum = 0;
printf("Please enter your choice from the following menu:\nA–Display all numbers\nB–Calculate the mean
and range\nC–Swap two numbers\nD–Replace a single number\nE–Repopulate full array\n");
scanf(" %c", &choice);
if (choice == 'A' || choice == 'a') {
for (i=0;i<9;i++)
printf("%d \t", n[i]);
}
else if (choice == 'b' || choice == 'B') {
for (i = 0; i < 9; i++)
printf("%d \t", n[i]);
for (i = 0; i < 9; i++)
sum += n[i];
printf("\nThe Mean of the array is: %.2f", sum / 9);
printf("\nThe range of the array is: %d", n[6] - n[8]);
}
else if (choice == 'c' || choice == 'C') {
int pos, post;
int x;
printf("\nWhich element of the above array do you want to swap? ");
scanf("%d %d", &pos, &post);
x = n[pos];
n[pos] = n[post];
n[post] = x;
for (i = 0; i < 9; i++)
printf("%d \t", n[i]);
}
else if (choice == 'd' || choice == 'D') {
int pos;
int x;
printf("\nPlease enter which array position you want to replace: ");
scanf("%d", &pos);
printf("\nPlease enter the number that needs to be replaced: ");
scanf("%d", &x);
n[pos] = x;
for (i = 0; i < 9; i++)
printf("%d \t", n[i]);
}
40