ASSISTANCE NEEDED!!! Thank you

I have a project to complete and i am new to c++ can you assist me.

Here is the question:

1) 1. Create a program that will take an integer number as input from the user and store the number into a two dimensional array, show the user the location of the array that value will be going into. Once the array is full, loop through and display the elements to the screen and finally display the highest number that was entered into the array.

The array will look like this

const int MAX_A = 3;
const int MAX_B = 4;
//declare two dimensional array
int numbers[MAX_A][MAX_B];

my code:


int main()
{
// declaring variables
using namespace std;
int MAX_A = 3;
int MAX_B = 4;

// asking the user to input data & storing in array
cout << "This program will take the user's input and store into an array." << endl;
cout << "Please enter any number, up to a series of 12 numbers..." << endl;
// declaring two-demensional array
int numberArray[MAX_A] [MAX_B];

for (int i=0; i < MAX_A; i++)
for (int j=0; j < MAX_B; j++)
{
cout<<"Enter a number:"<<endl;
cin>>numberArray[MAX_A][MAX_B];
{
if(numberArray>=0);
cout<<"Numbers:"<<numberArray[MAX_A] [MAX_B]<<endl;

if((MAX_A && MAX_B)<0);

cout<<"Error, Please enter a positive real number"<<endl;
break;

if((MAX_A && MAX_B)> 12);

cout<<"Error, you've entered more than 12 numbers"<<endl;
break;
}

}

System("PAUSE");
return 0;
code tags, please.
The index of an array goes from 0 to n-1,
cin>>numberArray[MAX_A][MAX_B]; Error: out of bounds
can u tell me the function to be used to calculate the size or the length of a 2 dimensional array ???
That would be MAX_A * MAX_B, no? Depending on how you define your array, you should always be able to get its dimensions just after initializing it.

-Albatross
ok so i dont want the user to input more than 12 numbers... in porder to do so in the if statement..

if( (MAX_A * MAX_B) > 12){

cout<<"Error, you've entered more than 12 numbers"<<endl;
// break;
system("PAUSE");


is that correct?
I hope this will get you started..

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

const int MAX_A = 3;
const int MAX_B = 4;
//declare two dimensional array
int numbers[MAX_A][MAX_B];

int main()
{
// declaring variables
    using namespace std;
    int MAX_A = 3;
    int MAX_B = 4;

// asking the user to input data & storing in array
    cout << "This program will take the user's input and store into an array." << endl;
    cout << "Please enter any number, up to a series of 12 numbers..." << endl;
// declaring two-demensional array
    int numberArray[MAX_A] [MAX_B];

    for (int i=0; i < MAX_A; i++)
        for (int j=0; j < MAX_B; j++)
        {
            cout << "Enter a number: ";
            cin >> numberArray[i][j];
        }

    //TODO: Your code here..

    return 0;
}
which editor/version do you use?
Do you mean IDE? I use both Code::Blocks and Visual C++
sizeof() is a built in function used for measuring the size of types etc etc... depending on the type of array and length(memory) of the type.
sizeof is not a function.
if( (MAX_A * MAX_B) > 12){

cout<<"Error, you've entered more than 12 numbers"<<endl;
// break;
system("PAUSE");
}

is that correct to find the length of the array?
int main()
{
// declaring variables
using namespace std;
int MAX_A = 3;
int MAX_B = 4;

// asking the user to input data & storing in array
cout << "This program will take the user's input and store into an array." << endl;
cout << "Please enter any number, up to a series of 12 numbers..." << endl;
// declaring two-demensional array

int numberArray[MAX_A] [MAX_B];
for (int i=0; i < MAX_A; i++)
for (int j=0; j < MAX_B; j++)

// the array takes in the numbers....
{
if(numberArray >=0 ){
cout<<"Enter a number:"<<endl;
cin>>numberArray[i][j];

}
if(MAX_A < 0 && MAX_B < 0 ){

cout<<"Error, Please enter a positive real number"<<endl;
// break;
}
else {


if( numberArray[i][j] > 12){

cout<<"Error, you've entered more than 12 numbers"<<endl;
// break;
system("PAUSE");
}
}
}
}


When i enter a number like 345.. it gives the output "error, you've entered more than 12 numbers"... help me fix this if statement please.

[code][/code] tags please.
cin>>numberArray[i][j]; You read a number and stored it in numberArray[i][j]
So the check of being a negative number must be against numberArray[i][j].

Your for loop will only allow MAX_A*MAX_B tries of input (whether they are correct or not)
if( sizeof(numberArray) > 12){

cout<<"Error, you've entered more than 12 numbers"<<endl;
}

this doesnt work explain why and how to fix please. Thanks
This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object.


Why not just have a for loop that has a cin >> and a cin.ignore() in it and not complain if the user inputs too many numbers, but rather just discard the remaining values?

-Albatross
Last edited on
Topic archived. No new replies allowed.