Loops and arrays

Write a program that receives the number of students the users wants to enter as an input. Next, the program should ask the users to enter the marks that each of the students received for the number of students specified earlier by the user.

Lastly, the program should display the student’s mark for the student that received the highest marks; the lowest mark; and the average mark.
Please help

[code]
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;
it is unlikely that any student actually got the average score.

that aside, you can get the high, low, and total as they are entered, you just need variables to store them. you will have to loop after to find anyone with the average, or median, or something close to it, or nothing at all, for that one.

eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do
{
std::cout << "Enter Mark: ";
std::cin >> Mark[count];

if(Mark[count] > Mark[biggest]) 
  biggest = count;

if(Mark[count] < Mark[smallest]) 
  smallest = count;
total+= Mark[count];
count++;

} while (count < sizeArry);


total /= (count-1);
now some sort of loop over Mark array for that last bit.. see if you can do it
Last edited on
Hello Wenzi,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting and line spacing it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

*** I found the second link to be the most help. ***



Looking over your instructions they feel rather lacking in content. It is best to pist the complete instructions that you were given. If this is what your were given than the program is less complex.

Although knowing the number of students you will have to work with I would just use a for loop.

Next you could call "count" "numOfStudents" or "numOfStuds" for better clarity of what the variable is for.

Although a "float" will work the preferred floating point variable type is a "double".

Since you should write your program in small pieces, compile and test often, this is an idea for you to start with.

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
33
34
35
36
37
38
39
40
41
42
43
44
// <--- Most common includes.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
//#include <vector>
//#include <cctype>

int main()
{
    constexpr unsigned int SIZEARRAY{ 5 };  // <--- Or I like using "MAXSIZE".
    constexpr int WIDTH{ 47 };              // <--- Width of line under heading.
    const std::string PROMPT{ "\n How many students will you be entering for?: " };

    unsigned numOfStuds{};
    double Mark[SIZEARRAY]{};
    //unsigned int count{ 0 };

    std::cout <<
        '\n'
        << std::string((WIDTH - 17) / 2, ' ')  // <--- Creates spaces to center heading.
                                               // The 17 is the width of the next line minus the (\n and \0) at the end.
        << "Heading if needed.\n "             // <--- Change as needed.
        << std::string(WIDTH, '-')             // <--- Creates a line of (-) under heading.
        << '\n';

    while (std::cout << PROMPT && !(std::cin >> numOfStuds))
    {
        if (!std::cin)
        {
            std::cerr << "\n     Invalid Input! Must be a number.";

            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
        }     
    }
	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point for testing. Comment or remove when finished.
}

The program needs some more planning first.

Andy

Edit: typo
Last edited on
Topic archived. No new replies allowed.