• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 21 pages
Exam (elaborations)

Arrays in C language

Document preview thumbnail
Preview 3 out of 21 pages

Arrays in C language

Content preview

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

Document information

Study
Unknown
Uploaded on
March 22, 2023
Number of pages
21
Written in
2022/2023
Type
Exam (elaborations)
Contains
Unknown
$11.60

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
0
Items
8
Last sold
3 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions