Array; Please help

I need to write a program that asks the user to enter any integer value. The program will store only the numbers that are divisible by 7 or divisible by 3 in an array. The program should declare a size of 10- if the number enter by the user is divisible by 3 or 7, then store it in the array-i need to allow the user to enter as many numbers as desired or until the array is full- Once the user completes the array size my program should First: display all the numbers in the array that are divisible by 3, their sum, and their average. (Hint: use a for loop that only displays and processes an array element only if ((data[i] %3) == 0) ) assuming the array’s name is data and the array’s subscript is i.)
b. Second: display all the numbers in the array that are divisible by 7, their sum, and their average. (Hint: use a for loop that only displays and processes an array element only if ((data[i] %7) == 0) ) assuming the array’s name is data and the array’s subscript is i.)
c. Third: display the sum and average of all numbers as well as the maximum and minimum.
Last edited on
I need to write a program that asks the user to enter any integer value.

When asking for help, you should also provide your efforts to solve whatever problem you're having. Nobody can help you with the code you aren't showing.
your right....this is what i have so far

#include <iostream>
using namespace std;

int main()
{
int numbers[10], sum = 0;
cout << "Enter 10 numbers: ";


for (int i = 0; i < 10; ++i)
{
cin >> numbers[i];
sum += numbers[i];

}

cout << "Sum = " << sum << endl;

return 0;
}
This may help get you on the right track:
http://ideone.com/w6oupQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
 
void print_divisibility(int value, int divisor) {
    bool evenly_divisible = value % divisor == 0;
 
    std::cout << value ;
    if (evenly_divisible)
        std::cout << " is evenly divisible by ";
    else
        std::cout << " is not evenly divisible by ";
 
    std::cout << divisor << '\n' ;
 
}
 
int main() {
    int value;
    while (std::cin >> value) {
        print_divisibility(value, 3);
        print_divisibility(value, 7);
    }
}
@cire, its not working. I had this problem before. I entered a function before the int main() and it would not work. What am i missing here?


#include <iostream>
using namespace std;

/* This program will asked the user to enter 10 numbers
that divisible by 3 and 7*/


int main ()
{
int hw[10];
int value;
float A3=0, A7=0, s1=0,s2=0;
int min=0,max=0;

do
{

cout <<" \nPlease enter 10 numbers that are divisible by 3 and 7\n\n ";
cin >> value;

cout <<"\n This is a valid number ";
value++;
return value;
}

while (value%3==0|| value%7==0);
{

cout << "\n\tThis number would not work ";
}


int count=0;
for (int x=0;x<10;x++)
{
if (hw[x]%3==0)
{
count++;
cout <<"\nNumbers divisible by 3= "<< hw[x];
cout << "\n\n";
s1= s1 + hw[x];
A3 =s1/count;
}
}
cout << "\nSum for 3= " << s1;
cout << "\nAverage for 3= " << A3;

count=0;
for ( int y=0;y<10; y++)
{
if (hw[y]%7 ==0)
{
count ++;
cout <<"\nNumbers are divisible by 7= " << hw[y];
cout << "\n\t\t\t\t";
s2= s2 + hw[y];
A7= s2/count;

}

}
cout << "\nSum for 7= " << s2 ;
cout << " \nAverage for 7= " <<A7;

max=hw[0];
for (int value=1;value<10;value++)
if (hw[value]>max)
max=hw[value];
cout << "\nThe Maximum is= " << max;

min=hw[0];
for (int value=1; value<10;value++)
if (hw [value]<min)
min=hw[value];
cout << "\nThe Minimun is = " << min;

cout <<"\n\t The complete sum is; " << s1 + s2 << endl;
cout <<"\t_____________________";
cout << "\n\t The complete average is; " << (float) (s1 + s2)/2<< endl ;
cout << "\t____________________\n";

cout << "\n\t\tThank you for using this program";


return 0;

}
Last edited on
closed account (48T7M4Gy)
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>

int main ()
{
    int hw[10] = {0};
    int value = 0;
    int counter = 0;
    
    std::cout << "Please enter 10 numbers that are divisible by 3 and/or 7\n";
    do
    {
        std::cin >> value;
        
        if (value % 3 == 0 or value % 7 == 0)
        {
            std::cout <<"This is valid number " << counter + 1 << " of 10\n";
            hw[counter] = value;
            counter++;
        }
        else
            std::cout << "Invalid number try another\n";
    } while (counter < 10);
    
    std::cout << "All 10 numbers entered\n";

    return 0;
}
Last edited on
Apologize for the formatting issues. When I paste on this website the originally formatted code changes.

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
55
56
57
58
59
60
61
62
#include <iostream>

int main()
{
	std::cout << "Enter 10 values divisable by 3 or 7:\n\n";
	constexpr unsigned short SIZE = 10;
	int array[SIZE] = { 0 };
    for (int &input : array)
		while (!(std::cin >> input) || !(input % 3 == 0 || input % 7 == 0))
		{
		    std::cout << "Invalid. Enter again: " << std::flush;
			std::cin.clear();
		    std::cin.ignore();
		}
	int sum1 = 0, sum2 = 0, count1 = 0, count2 = 0;
	for (unsigned short n = 0; n != 3; ++n)
	{
		std::cout << '\n';
		if (n == 0)
		{
		    std::cout << "Divisable by 3:\n";
		    for (int element : array)
				if (element % 3 == 0)
				{
				    std::cout << element << ' ' << std::flush;
					sum1 += element;
					++count1;
				}
			std::cout << "\nSum: " << sum1 << '\n' <<
                        "Average: " << sum1 / count1 << '\n';
		}
		else if (n == 1)
		{
		    std::cout << "Divisable by 7:\n";
	        for (int element : array)
				if (element % 7 == 0)
				{
					std::cout << element << ' ' << std::flush;
					sum2 += element;
					++count2;
				}
			std::cout << "\nSum: " << sum2 << '\n' <<
                        "Average: " << sum2 / count2 << '\n';
		}
		else
		{
		    std::cout << "All:\n";
		    int min = array[0], max = array[0];
		    for (int element : array)
		    {
				std::cout << element << ' ' << std::flush;
				min = min > element ? element : min;
				max = max < element ? element : max;
		    }
			std::cout << "\nTotal sum: " << sum1 + sum2 << '\n' <<
			"Total average: " << (sum1 + sum2) / SIZE << '\n';
                        std::cout << "Min: " << min << "\nMax: " << max << '\n';
		}
	}

	return 0;
}

Enter 10 values divisable by 3 or 7:

1
Invalid. Enter again: 2
Invalid. Enter again: 3
4
Invalid. Enter again: 5
Invalid. Enter again: 6
7
8
Invalid. Enter again: 9
10
Invalid. Enter again: 11
Invalid. Enter again: 12
13
Invalid. Enter again: 14
15
16
Invalid. Enter again: 17
Invalid. Enter again: 18
19
Invalid. Enter again: 20
Invalid. Enter again: 21
22
Invalid. Enter again: 23
Invalid. Enter again: 24

Divisable by 3:
3 6 9 12 15 18 21 24
Sum: 108
Average: 13

Divisable by 7:
7 14 21
Sum: 42
Average: 14

All:
3 6 7 9 12 14 15 18 21 24
Total sum: 150
Total average: 15
Min: 3
Max: 24
Last edited on
Hey, I have the same problem when i paste on the the form. But what is flush?
It is the same as endl, but flushes the stream (make sure everything is printed) without printing a newline.
oh thanks !

I wanted to improve my program and shorten the amount written inside the main. My math is wrong and the program is crashing at the end when its calculating the average of the input. My intentions were to average the inputs by the number of inputs like if all 3's were type then divide it by 10 to get 3. I getting 13 instead of 30( typing 3 ten times).

#include <iostream>
using namespace std;

/* This program will asked the user to enter 10 numbers
that divisible by 3 and 7 */

void message1 ( )
{
cout << " Hello, Welcome my last program of 2016!"<< endl;
cout << "****************************************";
}
void message ( )
{
cout << "\n\t\tThank you for using this program"<< endl;
cout << " Happy Holidays";
}
int SUM,sum,count;

void Aaverage ( int SUM, int sum)
{
cout << "\n\t The complete average is; " << (SUM + sum)/count <<endl ;
cout << "\t____________________\n";
}

void sumA( int firstNo, int secondNO)
{
cout <<"\n\t The complete sum is; " << SUM + sum << endl;
cout <<"\t_____________________";
}



int main ()
{
message1();

int A,B,array[10], num=0,counter=0;
float A3=0, A7=0, SUM=0,sum=0;
int min=0,max=0;

cout <<" \n\n\n\n\nPlease enter 10 numbers that are divisible by 3 and 7\n\n ";

do
{

cin >> num;
if (num%3==0 || num%7==0)
{
cout <<"\n This is a valid number " << counter+2 << " of 10\n";
array [10]=num;
counter++;
}
else
cout <<"\nThis is not a valid number";
}

while (counter< 10);

int count=0;
for (int i=0;i<10;i++)
{
if (num%3==0)
{
count++;
cout <<"\nNumbers divisible by 3= "<< num%3;
cout << "\n\n";
SUM= count + num;
A3 =num/count;
}
}
count=0;
for ( int i=0;i<10; i++)
{
if (num%7 ==0)
{
count ++;
cout <<"\nNumbers are divisible by 7= " << num;
cout << "\n\t\t\t\t";
sum= num + count;
A7= num/count;

}

}
max=array[10];
for (int num=1;num<10;num++)
if (array[10]>max)
max=array[10];


min=array[10];
for (int num=1; num<10;num++)
if (array[10]<min)
min=array[10];


cout << "\nSum for 3= " << SUM;
cout << "\nAverage for 3= " << A3;
cout << "\nAvegage for 7 = "<<sum;
cout << "\nSum for 7= " << A7;
cout << "\nThe Maximum is= " << max;
cout << "\nThe Minimun is = " << min;


sumA(SUM,sum);
Aaverage (SUM,sum);
message();


return 0;
}
closed account (48T7M4Gy)
Another way. Calculate as you go:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <limits>

int main ()
{
    int hw[10] = {0};
    
    int value = 0;
    int counter = 0;
    int counter_3 = 0;
    int counter_7 = 0;
    
    int sum = 0;
    int sum_3 = 0;
    int sum_7 = 0;
    
    int max = std::numeric_limits<int>::min();
    int min = std::numeric_limits<int>::max();
    
    std::cout << "Please enter 10 numbers that are divisible by 3 and/or 7\n";
    do
    {
        std::cin >> value;
        
        if (value % 3 == 0 or value % 7 == 0)
        {
            std::cout <<"This is valid number " << counter + 1 << " of 10\n";
            hw[counter] = value;
            sum += value;
            
            if (value < min ) min = value;
            
            if (value > max) max = value;
            
            if(value % 3 == 0)
            {
                sum_3 += value;
                counter_3++;
            }
            
            if(value % 7 == 0)
            {
                sum_7 += value;
                counter_7++;
            }
            
            counter++;
        }
        else
            std::cout << "Invalid number try another\n";
    } while (counter < 10);
    
    std::cout << "All 10 numbers entered.\n";
    
    std::cout << "                     Total of all numbers: " << sum << '\n';
    std::cout << "                   Average of all numbers: " << sum/(double)counter << '\n';
    std::cout << "                   Minimum of all numbers: " << min << '\n';
    std::cout << "                   Maximum of all numbers: " << max << '\n';
    
    if(counter_3 > 0)
    {
        std::cout << "  Total of all numbers divisible by 3: " << sum_3 << '\n';
        std::cout << "Average of all numbers divisible by 3: " << sum_3/((double)counter_3) << '\n';
    }
    else
        std::cout << "No numbers were divisible by 3.\n";
    
    if(counter_7 > 0)
    {
        std::cout << "  Total of all numbers divisible by 7: " << sum_7 << '\n';
        std::cout << "Average of all numbers divisible by 7: " << sum_7/((double)counter_7) << '\n';
    }
    else
        std::cout << "No numbers were divisible by 7.\n";
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.