Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Intro to Arrays In a new file, use the following syntax to declare an array to hold 5 integers. The purpose of the

Part 1: Intro to Arrays

  1. In a new file, use the following syntax to declare an array to hold 5 integers. The purpose of the array is to hold the grades of 5 students.

const int STUDENTS = 5;

int grades[STUDENTS];

In C, when an array is declared, it must be given a size. One way to do that is by passing the size in the [ ]. When the size is passed in the [ ], the size must be an integer, and it must be in the form of a constant. A constant is something that does not change during the running of the program (and therefore cannot be determined by the user.)

Constants may be literal constants: 5, 100, 2000

Or constants may be constant variables, a space reserved in memory whose contents cannot change during the running of the program. In the above code, STUDENTS is a constant variable. It is made constant with the keyword const and by using all caps in the identifier.

  1. Write a for loop to populate the array.

In Java, an array knows its size. We can access that size by accessing the length instance variable:

a.length //returns the size of the array in Java

However, in C, arrays do not store their size. Instead, any time you need to use the size of your array, you will use the size you declared in the constant.

Here the constant declared above is used as the test in a for-loop:

int i;

for (i = 0; i < STUDENTS; i++)

//Do stuff to the array

Recall that while Java places some form of zero into newly constructed array elements, C does not. The elements of the array grades contain junk.

Use a for-loop to populate your array with input from the user.

Your for loop should prompt the user for each students grade, identifying the student by number. Here is an example of the output.

Enter Person 1's grade: 40

Enter Person 2's grade: 50

Enter Person 3's grade: 60

Enter Person 4's grade: 100

Enter Person 5's grade: 100

  1. Add a while loop to the body of the for loop that will test for user input errors.

Declare a constant called MAX_GRADE and initialize it to 100. Use the while loop conditional to compare each user entry to MAX_GRADE. If the entered value is greater than MAX_GRADE, send the user an error message and prompt them to add a new number.

Enter Person 1's grade: 57

Enter Person 2's grade: 110

The highest grade possible is 100

Enter the correct grade: 100

Enter Person 3's grade: //etc . . .

If you wish, you may add some kind of a counter to the while loop that limits the number of errors the user can make before ending the program. This is not a requirement for this lab, but its a nice feature.

  1. Add to your program a function called arrayAvg()which takes as arguments an array and the size of the array and returns the average of the values of the array in the form of a float. Recall that in C, an array does not know its size, so the size will need to be passed to the function.

  1. Test your function in main().

Enter Person 1's grade: 80

Enter Person 2's grade: 70

Enter Person 3's grade: 60

Enter Person 4's grade: 90

Enter Person 5's grade: 100

The average score is 80.000000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago