Hey guys. can anyone help me, i'm a newbie and i need to show the number of times each of the numbers 1 -9 appear in a 2 dimensional array.
store counts are suppose to be 1 dimensional and i also have to enter the code to display the nine counts on the screen. im using visual c++ 2008 edition

What have you tried so far? Any thoughts on the subject? Do you know how to declar a multidimensional array, or a single-dimensional array for that matter? Do you know what a conditional (if) statement is (or what a switch statement is)? Do you have any code attempts written (if so post them here)?

//Displays the number of times a value
//appears in a two-dimensional array
//Created/revised by <your name> on <current date>

#include <iostream>


using std::cout;
using std::endl;


int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};



return 0;
}  //end of main function

that is what i have to start with so far. it's weird to me. it's a portion i have to do for a group project, but i'm not familar with programming, i do web design...lol

numberArray[1][1] = "value 2";
numberArray[1][2] = "value 4;
numberArray[1][3] = "value 1";
numberArray[1][4] = "value 2";
numberArray[1][5] = "value 2";
numberArray[1][6] = "value 1";
numberArray[1][7] = "value 1";
numberArray[1][8] = "value 1";
numberArray[1][9] = "value 1";

am I on the right path here?

numberArray[1][1] = "value 2";
numberArray[1][2] = "value 4;
numberArray[1][3] = "value 1";
numberArray[1][4] = "value 2";
numberArray[1][5] = "value 2";
numberArray[1][6] = "value 1";
numberArray[1][7] = "value 1";
numberArray[1][8] = "value 1";
numberArray[1][9] = "value 1";

am I on the right path here?

Not sure what you are doing here. numberArray is a 2-D array of strings? I'll comment on your last attempt. I doubt that you want to have an array of strings.

it's confusing to me altogether, it's like me learning a new language with no help.
see the code up top is what i have to start with. i have to include a code of how many times each of the numbers show up in the array, hence the reason i wrote the values portion to show how many times each number shows up. when you look at the code it shows you that it wants 5 rows and 3 columns which gives the soultion to this problem. it seems so simple for someone who knows how to put this togetehr, but i don't even know where to start, other than knowing what i'm suppose to do in words.

//Displays the number of times a value
//appears in a two-dimensional array
//Created/revised by <your name> on <current date>

#include <iostream>

using std::cout;
using std::endl;

int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};


return 0;
} //end of main function

Yes that's a correct way to fill in an array. Now you need to set up 9 counters (one each for the numbers 1 through 9). I'd suggest an array. Keep in mind that array indexes start at index 0, not index 1.

int count[10];

You have to make the array size one element larger than your largest index, which is 9. count[1] through count[9] will be used. You aren't keeping track of zeroes, right? If not, count[0] won't be used.

You'll want to initialize count[1] through count[9] to have values of 0 initially since you haven't seen any digits yet. Now traverse the 2-dimensional array.

int count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
     for (int j = 0; j < 3; j++)
     {
           // increment appropriate count element
           // based on the value of numbers[i][j]
     }
}

// put code to display count values here

Thanks, let me check this out and see what happens when i put it inot my program...I really appreciate you taking the time to help me.

it's confusing to me altogether, it's like me learning a new language with no help.
see the code up top is what i have to start with. i have to include a code of how many times each of the numbers show up in the array, hence the reason i wrote the values portion to show how many times each number shows up. when you look at the code it shows you that it wants 5 rows and 3 columns which gives the soultion to this problem. it seems so simple for someone who knows how to put this togetehr, but i don't even know where to start, other than knowing what i'm suppose to do in words.

See my last post. In your example array,

int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};

the digit 2 shows up 4 times. Thus if you implement my suggestion, after you are done with the nested for-loop, count[2] will contain the value 4. The code below will display that fact:

cout << "The digit " << 2 << " occurs " << count[2]
       << " times in the array." << endl;

Thus you do not need to use a string variable. You'll likely put the above code in a for-loop and replace the 2 above with a loop-control variable (such as i). Creating an array of strings is not necessary for this assignment.

Yes that's a correct way to fill in an array. Now you need to set up 9 counters (one each for the numbers 1 through 9). I'd suggest an array. Keep in mind that array indexes start at index 0, not index 1.

int count[10];

You have to make the array size one element larger than your largest index, which is 9. count[1] through count[9] will be used. You aren't keeping track of zeroes, right? If not, count[0] won't be used.

You'll want to initialize count[1] through count[9] to have values of 0 initially since you haven't seen any digits yet. Now traverse the 2-dimensional array.

int count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
     for (int j = 0; j < 3; j++)
     {
           // increment appropriate count element
           // based on the value of numbers[i][j]
     }
}

// put code to display count values here

Maybe i put the info in wrong, which would not surprise me, but i did get an error. am i suppose to be adding the values into the code you showed me?

I started with the following then I added the values the next time around and that's when i got the code not even to build, the first way it did pop up with the black processing screen..

int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};


int count[10];
// initialize count[1] through count[9] to equal 0


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}


// put code to display count values hereint count[10];
// initialize count[1] through count[9] to equal 0


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}


// put code to display count values here



return 0;
}  //end of main function



try 2


int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};


int count[10];
// initialize count[1] through count[9] to equal 0


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}


// put code to display count values hereint count[10];
// initialize count[1] through count[9] to equal 0


for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}


// numberArray[1][1] = "value 2";
numberArray[1][2] = "value 4;
numberArray[1][3] = "value 1";
numberArray[1][4] = "value 2";
numberArray[1][5] = "value 2";
numberArray[1][6] = "value 1";
numberArray[1][7] = "value 1";
numberArray[1][8] = "value 1";
numberArray[1][9] = "value 1";


return 0;
}  //end of main function

I started with the following then I added the values the next time around and that's when i got the code not even to build, the first way it did pop up with the black processing screen..

int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};

int count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}

// put code to display count values hereint count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}

// put code to display count values here


return 0;
} //end of main function


try 2

int main()
{
//declare numbers array
int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};

int count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}

// put code to display count values hereint count[10];
// initialize count[1] through count[9] to equal 0

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
// increment appropriate count element
// based on the value of numbers[j]
}
}

// numberArray[1][1] = "value 2";
numberArray[1][2] = "value 4;
numberArray[1][3] = "value 1";
numberArray[1][4] = "value 2";
numberArray[1][5] = "value 2";
numberArray[1][6] = "value 1";
numberArray[1][7] = "value 1";
numberArray[1][8] = "value 1";
numberArray[1][9] = "value 1";

return 0;
} //end of main function

Try 1 should compile. It will not give you good results when you run it since you need to change the commented code into real code, but it should compile.

int main()
{
    //declare numbers array
    int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};

    int count[10];
    // initialize count[1] through count[9] to equal 0

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // increment appropriate count element
            // based on the value of numbers[i][j]
        }
    }

    // put code to display count values hereint count[10];
    // initialize count[1] through count[9] to equal 0

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // increment appropriate count element
            // based on the value of numbers[i][j]
        }
    }

    // put code to display count values here


    return 0;
} //end of main function

The above compiled for me. Please don't forget to #include <iostream> and add the two lines regarding std::cout and std::endl that you had originally, since you'll need them later, but the above compiles without them.

Try 2 should not compile and it does not.

int main()
{
    //declare numbers array
    int numbers[5][3] = {{1, 2, 5} , {2, 4, 3}, {1, 9, 8}, {2, 6, 5}, {7, 4, 2}};

    int count[10];
    // initialize count[1] through count[9] to equal 0

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // increment appropriate count element
            // based on the value of numbers[i][j]
        }
    }

    // put code to display count values hereint count[10];
    // initialize count[1] through count[9] to equal 0

    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            // increment appropriate count element
            // based on the value of numbers[i][j]
        }
    }

    // numberArray[1][1] = "value 2";
    numberArray[1][2] = "value 4;
        numberArray[1][3] = "value 1";
    numberArray[1][4] = "value 2";
    numberArray[1][5] = "value 2";
    numberArray[1][6] = "value 1";
    numberArray[1][7] = "value 1";
    numberArray[1][8] = "value 1";
    numberArray[1][9] = "value 1";

    return 0;
}  //end of main function

You are using an array called numberArray which you have never declared. You cannot do that. You would have to add the following to the top of the program:

#include <string>
using std::string;
string numberArray[2][10];

This would cause all but two of your compile errors to go away in try 2. But as I mentioned in a previous post, I think you should stick with try 1. try 2 is not necessary in my opinion. I see no need for an array of strings for your task.

i'm gonna see what i can do here. i have to fix dinner for the kids, i'll be back monday if not this weekend. i'm getting caral tunnel sitting here all day..lol
like i said i'm new at this and have no idea what i'm suppose to do. it's strange they would even give me this problem.

i'm gonna see what i can do here. i have to fix dinner for the kids, i'll be back monday if not this weekend. i'm getting caral tunnel sitting here all day..lol
like i said i'm new at this and have no idea what i'm suppose to do. it's strange they would even give me this problem.

It does seem like too advanced of a problem to give to someone who is brand new to C++. Anyway, hope this helps and feel free to post again later.

Thanks a bunch, i enjoyed your help.. but i knew there was something a lil advanced for a newbie about this problem..

Oh, Lady Be Good :twisted:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.