100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Arrays in C language £7.99   Add to cart

Exam (elaborations)

Arrays in C language

 0 view  0 purchase

Arrays in C language

Preview 3 out of 21  pages

  • March 22, 2023
  • 21
  • 2022/2023
  • Exam (elaborations)
  • Unknown
All documents for this subject (8)
avatar-seller
micheleaccordino
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller micheleaccordino. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for £7.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

57114 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy revision notes and other study material for 14 years now

Start selling
£7.99
  • (0)
  Add to cart