Small comma issue in output

Hello, I'm having a minor issue and it's bugging me. I'm supposed to ask the user for 10 numbers and then print their odd and even numbers inputted and then add it. I want to remove the comma from the last number.
However; when it prints I get;
The odd numbers you entered were: 1, 3, 5, 7, 9,
Instead of me wanting to get;
The odd numbers you entered were: 1, 3, 5, 7, 9
How do I go about fixing that? This is something I'm supposed to know but I'm having a brain fart.
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 #include <iostream>

using namespace std;

const int i = 10;

void userInput(int myArray[i]);
void printOddNumbers(const int myArray[i]);
void printEvenNumbers(const int myArray[i]);

int main()
{
    int myArray[i];
    
    userInput(myArray);
    printOddNumbers(myArray);
    printEvenNumbers(myArray);
    
    return 0;
    
}

void userInput(int myArray[i])
{
	int index = 0;

	cout << "Enter 10 numbers:" << endl;
	
	for(int row = 0; row < i; ++row)
	{
		cout << ++index << ". Enter a number: ";
		cin >> myArray[row];
	}
	
	cout << endl;
}

void printOddNumbers(const int myArray[i])
{
	int oddSum = 0;
	
	cout << "The odd numbers you entered were: ";
	
     for(int row = 0; row <= i; row++)
     {
        if (myArray[row] % 2==1)
        {
            cout << myArray[row] << ", ";
        }
        
     }
	
	cout << endl;
	
	for (int row = 0; row <= i; row++)
	{
		if (myArray[row] % 2==1)
		{
			oddSum += myArray[row];
			
		}

	}
	
	cout << "Their sum is: " << oddSum << endl;
	
}

void printEvenNumbers(const int myArray[i])
{
    int evenSum = 0;
    
    cout << "The even numbers you entered were: ";
    
     for(int row=0; row < i; row++)
     {
        if(myArray[row] % 2==0)
        {
            cout << myArray[row] << ", ";
        }
      }
      
    cout << endl;
    
    for (int row = 0; row < i; ++row)
    {
		if (myArray[row] % 2==0)
		{
			evenSum += myArray[row];
			
		}
		
    }
        
    cout << "Their sum is: " << evenSum << endl;
        
}
Last edited on
i is a bad name for a non-iteration variable.
But instead of comparing row < i on line 85, compare row < i - 1.
Then, after the for loop, if i > 0, print the final element, myArray[i - 1].
Last edited on
Hello av16352,

This is a rework of your program for you consideration and comparison. See what you think.
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>

using namespace std;  // <--- Best not to use.

constexpr int MAXSIZE{ 10 };

void userInput(int myArray[MAXSIZE]);              // <--- Size is not requited for a 1D array or the 1st diminsion of a 2D.
void printOddNumbers(const int myArray[MAXSIZE]);  // <--- Size is requited for the 2nd diminsion of a 2D array. OK if you leave.
void printEvenNumbers(const int myArray[MAXSIZE]);

int main()
{
    // <--- Switch comments for testing.
   // int myArray[MAXSIZE]{};  // <--- ALWAYS initialize your variables.
    int myArray[MAXSIZE]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    //int myArray[MAXSIZE]{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    //userInput(myArray);  // <--- Skipped for testing. We already know it works.

    printOddNumbers(myArray);

    printEvenNumbers(myArray);

    return 0;

}

void userInput(int myArray[MAXSIZE])
{
    int index = 0;

    cout << "\nEnter 10 numbers:\n";

    for (int row = 0; row < MAXSIZE; ++row)
    {
        cout << ++index << ". Enter a number: ";
        cin >> myArray[row];
    }

    cout << "\n";
}

void printOddNumbers(const int myArray[MAXSIZE])
{
    int oddSum{};
    int end{ !(myArray[MAXSIZE - 1] % 2) ? MAXSIZE - 2 : MAXSIZE - 1 };

    cout << "The odd numbers you entered were: ";

    for (int row = 0; row < MAXSIZE; row++)
    {
        if (myArray[row] % 2)
        {
            cout << myArray[row] << (row < end ? ", " : "");

            oddSum += myArray[row];
        }
    }

    cout << "\n";

    //for (int row = 0; row < MAXSIZE; row++)
    //{
    //    if (myArray[row] % 2)
    //    {
    //        oddSum += myArray[row];
    //    }
    //}

    cout << "Their sum is: " << oddSum << "\n";
}

void printEvenNumbers(const int myArray[MAXSIZE])
{
    int evenSum{};
    int end{ myArray[MAXSIZE - 1] % 2 ? MAXSIZE - 2 : MAXSIZE - 1 };

    cout << "\nThe even numbers you entered were: ";

    for (int row = 0; row < MAXSIZE; row++)
    {
        if (!(myArray[row] % 2))
        {
            cout << myArray[row] << (row < end ? ", " : "");

            evenSum += myArray[row];
        }
    }

    cout << "\n";

    //for (int row = 0; row < MAXSIZE; ++row)
    //{
    //    if (!(myArray[row] % 2))
    //    {
    //        evenSum += myArray[row];
    //    }
    //}

    cout << "Their sum is: " << evenSum << "\n";
}

You do not need the extra for loops just to sum the numbers. You can do that in the if statement when you are already there.

Andy

Edit: typo
Last edited on
@Andy

 
 int end{ !(myArray[MAXSIZE - 1] % 2) ? MAXSIZE - 2 : MAXSIZE - 1 };


No assumptions can be made about the ordering/number of even/odd entered numbers. This fails for an input of 2 2 2 2 3 3 3 3 3 3 displaying a final unwanted ,

Consider:

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
#include <iostream>

constexpr int MAXSIZE {10};

void userInput(int myArray[MAXSIZE]);
void printOddNumbers(const int myArray[MAXSIZE]);
void printEvenNumbers(const int myArray[MAXSIZE]);

int main()
{
	int myArray[MAXSIZE] {};

	userInput(myArray);
	printOddNumbers(myArray);
	printEvenNumbers(myArray);
}

void userInput(int myArray[MAXSIZE])
{
	std::cout << "\nEnter " << MAXSIZE << " numbers:\n";

	for (int row = 0; row < MAXSIZE; ++row) {
		//std::cout << row + 1 << ". Enter a number: ";
		std::cin >> myArray[row];
	}

	std::cout << '\n';
}

void printOddNumbers(const int myArray[MAXSIZE])
{
	int oddSum {};

	std::cout << "The odd numbers you entered were: ";

	for (int row = 0, rest = 0; row < MAXSIZE; ++row)
		if (myArray[row] % 2) {
			std::cout << (rest++ ? ", " : "") << myArray[row];
			oddSum += myArray[row];
		}

	std::cout << "\nTheir sum is: " << oddSum << '\n';
}

void printEvenNumbers(const int myArray[MAXSIZE])
{
	int evenSum {};

	std::cout << "\nThe even numbers you entered were: ";

	for (int row = 0, rest = 0; row < MAXSIZE; ++row)
		if (!(myArray[row] % 2)) {
			std::cout << (rest++ ? ", " : "") << myArray[row];
			evenSum += myArray[row];
		}

	std::cout << "\nTheir sum is: " << evenSum << "\n";
}



Enter 10 numbers:
2 2 2 2 3 3 3 3 3 3

The odd numbers you entered were: 3, 3, 3, 3, 3, 3
Their sum is: 18

The even numbers you entered were: 2, 2, 2, 2
Their sum is: 8

Last edited on
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
#include <iostream>
#include <valarray>
using namespace std;

template<typename T> void userInput( valarray<T> &V )
{
   cout << "Enter " << V.size() << " numbers:\n";
   for ( T &e : V ) cin >> e;
}

template<typename T> ostream & operator << ( ostream &out, const valarray<T> &V )
{
   if ( V.size() )
   {
      out << V[0];
      for ( int i = 1; i < V.size(); i++ ) out << ", " << V[i];
   }
   return out;
}

int main()
{
   const int N = 10;
   valarray<int> V( N );
   userInput( V );
   valarray<int> odd( V[V%2==1] ), even( V[V%2==0] );
   cout << "The odd numbers are " << odd << " and their sum is " << odd.sum() << '\n';
   cout << "The even numbers are " << even << " and their sum is " << even.sum() << '\n';
}


Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
The odd numbers are 1, 3, 5, 7, 9 and their sum is 25
The even numbers are 2, 4, 6, 8, 10 and their sum is 30
Last edited on
Topic archived. No new replies allowed.