Hello since I am new to c++ we just discussed Arrays about a week and a half ago and I tried to understand how to display them but I'm just confused. SO we are suppose to declare array items as:
prints out the array in order, skipping any item that is evenly divisible by 3, and then declares the largest item in the array including numbers not divisible by 3 In other words, the program should output all numbers not divisible by 3 in reverse order and then print
The program also has to output the largest item in the array. So idk if I have to declare a random number to a variable to get those 10 values.
Ok so I changed a few things like you guys said. I have the computer outputting the largest number... I wrote the code to display numbers that are NOT divisible by 3 in reverse order but I have errors.
#include <iostream>
usingnamespace std;
int main ()
{
constint num_size = 10;
int items[num_size] = {42,35,15,48,96,55,45,27,93};
int largnum = 0;
for(int i = 0; i < num_size; i++)
{
if(items[i]>largnum)
largnum = items [i];
}
cout << "The largest number in the array is: " << largnum << endl;
if(items[i] % 3 != 0)
{
cout << "Numbers that are not divisble by 3: " << items[i] << endl;
}
return 0;
}
'i' is undefined in your second if statement, because it is outside the scope of the first i. Also, you're going to need to loop through the array again to find the numbers not divisible by 3.
#include <iostream>
usingnamespace std;
int main()
{
constint num_size = 10;
int items[num_size] = { 42, 35, 15, 48, 96, 55, 45, 27, 93 };
int largnum = 0;
for (int i = 0; i < num_size-1; i++)
{
if (items[i]>largnum)
largnum = items[i];
}
cout << "The largest number in the array is: " << largnum << endl;
for (int j = 0; j < num_size-1; j++)
{
if (items[j] % 3 != 0)
cout << "Numbers that are not divisible by 3: " << items[j] << endl;
else;
}
system("pause");
return 0;
}
Thanks it works! I am still trying to learn though what does it mean when you wrote this:
1 2 3
for (int j = 0; j < num_size-1; j++)
Also I am trying to print out numbers that are divisible by 3 so: 21 42 15 48 96 45 27 93... I was thinking of using another for loop with a different variable so:
1 2 3 4 5 6 7 8 9 10
for (int x = 0; x < num_size -1; m++)
{
if (items[m] % 3 = 0)
cout << "Numbers divisible by 3 are: 21 42 15 48 96 45 27 93" << endl;
else;
}
for (int j = 0; j < num_size-1; j++) is the same as any other for loop. I just like to use different variables every time so I remember what I'm referring to.
Okay, so num_size-1 is because the size of the array is 1 greater than the last element. The size starts at 1, whereas the first element starts at 0. So if you have 10 values, their position in the array would be 0 through 9. The size of the array would be 10. SO!!!!! value 0 is at position 1. Value 1 is at position 2. Does this make sense?
Your statement is not correct. why did you change x to m? m is undefined. I think you know what to change it to. ;-)
@disturbedfuel: That could be true if you used <=.
If you use a simple <, you shouldn't subtract, or the last value in the array will never be reached.
Anyways, I'm one of those who prefers you to have a valid starting value for your largnum. int largnum = items[0]; // line 13
So much for "added complexity", your program is now negative-integers-aware, with just 7 characters.