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.
Put the cod
#include <iostream>
usingnamespace 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.
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.
#include <iostream>
usingnamespace 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;
}