Displaying array program, simple program but confused!

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:

 
int items[10] = { 21, 42, 35, 15, 48, 96, 55, 45, 27, 93 };


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.
What have you tried? To get the largest number, set your max value to the smallest value you can think of before starting the program ex

1
2
3
#include <limits>
...
int max = std::numeric_limits<int>::min();


Compare your values to the max and update max accordingly
Last edited on
Or you could just assign items[0] to maximum and check that against the rest of the elements, changing the value of maximum as appropriate.
OK so far I got this I have the computer output what the biggest number is : I just don't know how to print numbers divisible by 3 in reverse order...

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

#include <iostream>

using namespace std;

int main ()


{
    int items[10] = {42,35,15,48,96,55,45,27,93};
    
    int largnum = 0;

    for(int i=0;i<5;i++)
    
    {
        
	if(items[i]>largnum)
        
	largnum = items [i];
    }
    
    cout << "The biggest number is: " << largnum << endl;
    
    return 0;
}
Last edited on
Why are you only looping 5 times?

EDIT: Sorry, I'll be more thorough.

Why not loop your full array??
Why not declare a constant integer for the size of your array so you can change it easier later?

Up top, put something like:
const int NUM_SIZE = 10;

Then, replace int items[10] with int items[NUM_SIZE]

Then, replace for(int i=0; i<5; i++) with for(int i = 0; i < NUM_SIZE; i++)
Last edited on
i suggest you to initial largnum as items[0], because if the elements in items are all less than 0, the out put will be 0;


if a value x is divisible by 3, x%3 = 0


so:
if((items[i] % 3 == 0){
items[i] is divisible by 3;
}
Last edited on
i suggest you to initial largnum as items[0], because if the elements in items are all less than 0, the out put will be 0;


None of his numbers are less than 0. He told us what they were above. Try not to add confusion.
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.

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

#include <iostream>

using namespace std;

int main ()


{
    const int 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.

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()


{
	const int 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;
}


Would that be correct
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. ;-)
Got it! Thanks so much for explaining! Hopefully I don't run into this problem again
@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.
Topic archived. No new replies allowed.