Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ doesn't give me an output

I’m trying to loop through a 2d array and find the sum of some numbers but for some reason, I don’t get any output from the compilier.
Here is my code:

#include <iostream>
using namespace std;

int main()
{
    int arr[3][3] = { {2,5,7},
                      {3,6,8},
                      {5,8,6} };

    int oddSum = 0;
    int evenSum = 0;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            while (arr[i][j] != 2 && arr[i][j] % 2 == 0)
            {
                evenSum += arr[i][j];
            }

            while (arr[i][j] != 3 && arr[i][j] % 3 == 0)
            {
                oddSum += arr[i][j];
            }
        }
    }

    cout << "oddSum = " << oddSum << endl;
    cout << "evenSum = " << evenSum << endl;

    return 0;
    
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As arr[i][j] is not updated within your while loops, if that condition is true, it will never be false and the loop will never exit.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading