Having a problem with finding the smallest number in an array.

Hello i need help regarding this one assignment of mine. I have to cout the smallest element in the array for each of the mark (1-5) but I didn't proceed with mark2 - mark5 because I'm stuck at mark1. Here is my code so far, I really don't know why it doesn't work. Would be glad if someone can help me. Thanks in advance!

#include <iostream>
using namespace std;

int main ()
{
int mark1[40] , mark2[40] , mark3[40], mark4[40], mark5[40],i,j,l,lowmark;
int studentid[40];

for(i=0 ; i<40 ; i ++)
{
cout<<"Enter student id (enter -999 to stop)"<<endl;
cin>> studentid[i];
cout<<endl;
if(studentid[i]== -999)
break;
cout<<"Enter mark for quiz 1:"<<endl;
cin>> mark1 [i];
cout<<"Enter mark for quiz 2:"<<endl;
cin>>mark2[i];
cout<<"Enter mark for quiz 3:"<<endl;
cin>>mark3[i];
cout<<"Enter mark for quiz 4:"<<endl;
cin>>mark4[i];
cout<<"Enter mark for quiz 5:"<<endl;
cin>>mark5[i];
cout<<endl;
}

cout<<"Student "<<"Quiz1 "<<" Quiz 2"<<" Quiz 3"<<" Quiz 4"<<" Quiz 5"<<endl<<endl;

for(j=0;j<i;j++)
{
cout<<studentid[j]<<" "<<mark1[j]<<" "<<mark2[j]<<" "<<mark3[j]<<" "<<mark4[j]<<" "<<mark5[j]<<endl<<endl;
}

lowmark = mark1[0];
for(i = 0; i < 40; i++)
{
if(mark1[i] < lowmark)
{
lowmark = mark1[i];
}
}
cout<<lowmark;



}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
to make it easy, lets do this:
int* marks[6]{nullptr,mark1,mark2,mark3,mark4,mark5};
and extend lowmark:
int lowmarks[6];
and its a lot like what you have...
for(i = 1; i < 6; i++)//1,2,3,4,5
 lowmarks[i] = marks[i][0]; 

and the other for loop becomes
for(i = 1; i < 40; i++)
{
   for(j =1; j<6; j++) 
    if(lowmarks[j] < marks[j][i])
      lowmarks[j] = marks[j][i];
}

which brings up a couple of points...
I used 6 so that marks[1] is marks1 which is easy to remember and follow. its confusing if marks[0] is marks1.
which implies that marks1 should be marks0 ... number things from zero in c++!
and the other thing is... do not make a bunch of the same thing with slight name variations, make an array of them (later, a vector).
I backfed your marks into an array to make it like that, but ideally, marks would have been a 2d array to begin with. If you had done that, your cin loop would not repeat the same code 5 times, but would be like my above loop, using the 'array of mark arrays' to reduce that code to a couple of lines.

finally, your code has a bug. you let the user stop typing with a 999 but you don't retain this information. That means that the processing loops look at invalid data. you should keep a count of how many they entered and only look for the lowest in the valid input range... This is probably why it does not work, I am willing to bet you stopped before typing 40 values in :)
on that note, you can make a text file of 40 values and feed it to the program using the < operator on the command line when you run the program, or IDE will let you feed it from there, if you want to test 40 numbers, or you can put in random values in a range. Or the easy way out, change 40 to 5 and test with the reduced size until it works then upgrade the size again. which leads to... make the numbers a constant so you only have to change the 40 in one place to do that idea (best practice, always name constants, do not just have 37 and 98 and whatnot embedded in code here and there with no clue what those numbers refer to). If the constant is math or something, a comment is ok instead, eg x = sqrt(2.0)*y; //sqrt 2 is because some formula for what I am doing here type comment.

Last edited on
Sorry idk if i can reply here bcs im new to this site but this is to jonnin.
First of all, thank you! for replying this late at night, it was 12.47a.m. here when you replied.

i may have forgot to state that the question included that the user may input data for up to 40 students, hence why I set the limit to 40.
And yes you're correct I stopped before putting 40 values in for each of the arrays. :(
I think that one possible fix for this is to let the user set the array to be any size but I will state until at most 40.. I mean that still answers the question in my opinion.
But for this fix, the user must fulfill the amount they themselves input right?
I use the -999 as a sentinel stop for the for loop, and because I thought that it could work like how you stop a loop statement.
But since it involves an array of a fixed size, I guess it can cause a failure if stopped halfway.

Also thanks for the suggestion on testing with reduced size cause I keep forgetting this! It's better to test small before actually using the final size like you said.

I'm sorry if I don't fully understand your comment and might repeat the same thing you say about in this reply and also maybe some of my terms I used in this reply are wrongly put cause I'm just starting and I'm very slow at this! again, thanks for taking the time to reply! ;~;
arrays have a fixed size. you are doing it right, its just not complete. you need to track where they quit and keep that value, and use it later, is all.

-999 is fine for this.

basically you are doing it right.
add this:
int stopval{40}; //if they do not hit the if/break below, it will be the whole array.
if(studentid[i]== -999)
{
stopvalue = i;
break;
}
and then in the later loops.. instead of 40, say
for(i = 0; i < stopval; i++)

if anything I said confused you, ask.
Last edited on
Perhaps consider:

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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <limits>
#include <algorithm>

int main()
{
	constexpr size_t nomrks {5};
	constexpr size_t maxstud {40};

	int marks[nomrks][maxstud] {};
	size_t nostud {};
	int studentid[maxstud] {};
	int lowmark[nomrks];

	std::fill_n(lowmark, nomrks, std::numeric_limits<int>::max());

	for (; nostud < maxstud; ++nostud) {
		std::cout << "Enter student id (enter -999 to stop): ";
		std::cin >> studentid[nostud];

		if (studentid[nostud] == -999)
			break;

		for (size_t q = 0; q < nomrks; ++q) {
			std::cout << "Enter mark for quiz " << q + 1 << ": ";
			std::cin >> marks[q][nostud];
			if (marks[q][nostud] < lowmark[q])
				lowmark[q] = marks[q][nostud];
		}
	}

	std::cout << "\nStudent  ";
	for (size_t s = 0; s < nomrks; ++s)
		std::cout << "Quiz " << s + 1 << "  ";

	std::cout << '\n';

	for (size_t j = 0; j < nostud; ++j) {
		std::cout << std::setw(11) << std::left << studentid[j];

		for (size_t q = 0; q < nomrks; ++q)
			std::cout << std::setw(8) << std::left<< marks[q][j];

		std::cout << '\n';
	}

	std::cout << "\nLowest scores are:\n";

	for (size_t l = 0; l < nomrks; ++l)
		std::cout << "Quiz " << l + 1 << ": "<< lowmark[l] << '\n';

	std::cout << '\n';
}

Topic archived. No new replies allowed.