homework help please?

Jun 24, 2018 at 2:03am
so we are doing for loops and he wants us to make a code that shows us the biggest number and the smallest number in this using a for loop but it keeps say large is not being int and it clearly is i believe what im i doing wrong? heres my code.

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
  Put the cod
#include <iostream>

using namespace std;

int main()
{

	int n;
	int num;
	int large;
	int small;

	cout << "how many numbers do you want to enter" << endl;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cout << "enter a number" << endl;
		cin >> num;
		if (i==0)
		{
			large == small == num;
			
			
		}
		if (num<small)
		{
			small = num;
			

		}
	}
	cout << "the largest number was  " << large << endl;
	cout << "the smallest number was " << small << endl;
	return 0;
}e you need help with here.
Last edited on Jun 24, 2018 at 2:04am
Jun 24, 2018 at 2:31am
large == small == num;

== is a comparison, a == b says "is a the same as b"


= is assignment, a = b says set a's value to b's value.

you don't yet have logic to find large but you are on the right track
Last edited on Jun 24, 2018 at 2:32am
Jun 24, 2018 at 2:48am
you have to intialize some value of large and small. consider this, the first time your code runs it checks if(num<small) but you haven't assigned any value to small, I believe that's what you tried to do in if(i==0) block. but as @jonnin pointed out, "==" is different than "="

- "==" is used to compare two numbers if they are equal or not. while
- "=" is used to assign the value.

Now, back to your code. Your logic for small seems right , but you also have to find large. So, check against if(num<small) i.e else if(num>large) and assign large.

change if(i==0) block with:
1
2
3
4
5
if (i==0)
	{
		large = small = num;
			
	}

and add after checking for small

1
2
3
4
else if(num>large)
	{
		large=num;
	}


And you will get your result.
Last edited on Jun 24, 2018 at 2:49am
Jun 24, 2018 at 5:28am
tyler21210 here is the solution:
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

#include <iostream>

using namespace std;

int main()
{
	int m;
	int n;
	int num;
	int large;
	int small;

	cout << "how many numbers do you want to enter?" << endl;
	cin >> n;
	cout<< "Enter Num 1:"<<endl;
	cin>> m;
	large=m;
	small=m;
	int d=2;
	for (int i = 0; i < n-1; i++)
	{	
		cout << "Enter Num "<<d<<":" << endl; //here d will show the output like this: Enter Num: 2, Enter Num: 3 . etc.
		cin >> num;
		
			if(num>large)
			large=num;
			 if(num<small)
			small=num;
			d++;		
	}
cout << "the largest number was  " << large << endl;
	cout << "the smallest number was " << small << endl;
	return 0;
}

Must Compile This Code
Last edited on Jun 24, 2018 at 5:33am
Topic archived. No new replies allowed.