do while loop and arrays...

I'm trying to solve this question.

Write a program based on the requirements given below using c++ command.
a. Get five floating point numbers from the user in main() and store the numbers
in an array called Mark.(use do while loop)
b. Call function display(...)in main() and send array Mark.
c. Display the number and total mark in function display(...)using for loop.

Below is display..
Enter Mark : 10
Enter Mark : 20
Enter Mark : 30
Enter Mark : 40
Enter Mark : 50

Mark = 10
Mark = 20
Mark = 30
Mark = 40
Mark = 50

Total Salary= 150
Press any key to continue

Thank you.
 
 
Last edited on
What have you done so far?

Break your program into small pieces. Figure out how to do each piece separately.

Part (a) says get five floating points from the user and store them in an array. Let's break that down even more.
Do you know how to get a single floating-point number from the user?
If not, read http://www.cplusplus.com/doc/tutorial/basic_io/ and go down to the Standard Input section.

You also need to make array with 5 elements in it.
Do you know how to make an array?
If not, read http://www.cplusplus.com/doc/tutorial/arrays/

Try it, and post your attempt.

It will also be helpful for you to use a for loop in order to loop the input 5 times.
But since you require a do-while loop, this can be like this:
1
2
3
4
5
6
int i = 0;
do {
    // get input from user (see Standard Input tutorial)
    i++;
} while (i < 5);
Last edited on
To give you an idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

void display(float[], const unsigned int);

int main()
{
	const unsigned int sizeArry{ 5 };
	float Mark[sizeArry]{};
	unsigned int count{ 0 };

	do
	{
		std::cout << "Enter Mark: ";
		std::cin >> Mark[count];
		count++;

	} while (count < sizeArry);
	std::cout << std::endl;
	display(Mark, sizeArry);
}

void display(float Mark[], const unsigned int sizeArry)
{
	float total{};
	for (unsigned int count = 0; count < sizeArry; count++)
	{
		std::cout << "Mark = " << Mark[count] << std::endl;
		total += Mark[count];
	}

	std::cout << "\nTotal Salary= " << total << std::endl;
}
OK, thanks a lot both of you
Last edited on
Topic archived. No new replies allowed.