473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

array inside an array in C++

Hi, what I am trying to do in this program is to save the numbers enter by the user into the array data but with any positive result. This is what I got so far. Thanks for any help.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void printHeading();
  7.  
  8. int main()
  9. {
  10.     cout<<endl;
  11.     char choice;
  12.     int rows=5;
  13.     int columns=5;
  14.     int array[5][5]={{2,13,23,45,50},
  15.                              {5,12,34,36,51},
  16.                              {2,23,25,46,54},
  17.                              {2,7,35,39,48},
  18.                              {3,5,26,39,49}};
  19.  
  20.     int newArray[5];
  21.     printHeading();
  22.  
  23.     for(int i=0; i<rows; i++)
  24.     {
  25.         cout<<setw(33)<< i <<")";
  26.         for(int j=0; j<columns; j++)
  27.         {
  28.             cout<<setw(8)<<array[i][j];
  29.         }
  30.         cout<<endl;
  31.     }
  32.     do
  33.     {
  34.         cout<<"\n    Enter 5 numbers: ";
  35.         for(int i=0; i< 5; i++)
  36.         {
  37.             cin>>newArray[i];
  38.         }    
  39.         cout<<endl;
  40.         printHeading();
  41.         for(int i=0; i<rows; i++)
  42.         {
  43.             cout<<setw(33)<< i <<")";
  44.             for(int j=0; j<columns; j++)
  45.             {
  46.                 newArray[ array[i][j] ]=array[i][j]; // saving the array data into the newArray
  47.                 cout<<setw(8)<<newArray[ array[i][j] ];             
  48.             }
  49.             cout<<endl;
  50.          }
  51.  
  52.     cout<<"\n\n    Would you like to try again? (y/n): ";
  53.     cin >> choice;
  54.  
  55.     }  while (choice == 'y'|| choice == 'Y');
  56.     cout<<endl;
  57.     return 0;
  58. }
  59.  
  60. void printHeading()
  61. {
  62.     cout<<setw(82)<<"=====================================================\n\n";
  63.     cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
  64.     cout<<setw(81)<<"-----------------------------------------------------\n";
  65. }
Aug 28 '10 #1
8 5507
whodgson
542 512MB
Use your j for loop to overwrite the 2d data under l 28 and do away with newArray[]
Aug 28 '10 #2
Hi whodgson, I think I did what you told me and it works, but not the way I want because the five numbers I entered replaced the first row of the array data and what I want is add it to the array data. This is what I got so far:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. const int rows=5;
  5. const int columns=5;
  6.  
  7. using namespace std;
  8.  
  9. void printHeading();
  10.  
  11. int main()
  12. {
  13.     cout<<endl;
  14.     char choice;
  15.     int array[rows][columns]={{2,13,23,45,50},
  16.                               {5,12,34,36,51},
  17.                               {2,23,25,46,54},
  18.                               {2,7,35,39,48},
  19.                               {3,5,26,39,49}};
  20.  
  21.     printHeading();
  22.  
  23.     for(int i=0; i<rows; i++)
  24.     {
  25.         cout<<setw(33)<< i <<")";
  26.         for(int j=0; j<columns; j++)
  27.         {
  28.             cout<<setw(8)<<array[i][j];
  29.         }
  30.         cout<<endl;
  31.     }
  32.     do
  33.     {
  34.         cout<<"\n    Enter 5 numbers: ";
  35.  
  36.         for(int i=0; i<1; i++)
  37.         {
  38.             for(int j=0; j<columns; j++)
  39.             {
  40.                cin>>array[i][j];
  41.             }
  42.         }
  43.         cout<<endl;
  44.         printHeading();
  45.         for(int i=0; i<rows; i++)
  46.         {
  47.             cout<<setw(33)<< i <<")";
  48.             for(int j=0; j<columns; j++)
  49.             {
  50.                 cout<<setw(8)<<array[i][j];
  51.             }
  52.             cout<<endl;
  53.          }
  54.     cout<<"\n\n    Would you like to try again? (y/n): ";
  55.     cin >> choice;
  56.  
  57.     }  while (choice == 'y'|| choice == 'Y');
  58.     cout<<endl;
  59.     return 0;
  60. }
  61.  
  62. void printHeading()
  63. {
  64.     cout<<setw(82)<<"=====================================================\n\n";
  65.     cout<<setw(76)<<"ROWS     (0)     (1)     (2)     (3)     (4)\n";
  66.     cout<<setw(81)<<"-----------------------------------------------------\n";
  67. }
  68.  
Can you help me. THANKS
Aug 29 '10 #3
whodgson
542 512MB
That was probably because of line 40.
Use an if statement/s in the body of the j loop like:
Expand|Select|Wrap|Line Numbers
  1. cin>>num;//get a number from user
  2. if(i==0 && j==2)array[i][j]=num;//overwrite with  num in 1st row and 3rd column.
Repeat for remaining input.
Aug 30 '10 #4
I am not sure if you understood my problem. This is my output from the last code:


ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
0) 2 13 23 45 50
1) 5 12 34 36 51
2) 2 23 25 46 54
3) 2 7 35 39 48
4) 3 5 26 39 49

Enter a numbers: 1 1 1 1 1

================================================== ===

ROWS (0) (1) (2) (3) (4)
-----------------------------------------------------
0) 1 1 1 1 1
1) 5 12 34 36 51
2) 2 23 25 46 54
3) 2 7 35 39 48
4) 3 5 26 39 49

First row was replaced by the numbers entered by the user.
But if you understood what it really happen, then is me who can not understand what you trying to explain me . Can you tell me what happen. Once again THANKS
Aug 30 '10 #5
Is there anybody can help me out to understand what is wrong in my program. Thanks
Aug 30 '10 #6
donbock
2,426 Expert 2GB
When the user enters a set of 5 numbers, do you want that set to replace one of the rows in the array or do you want that set to be appended to the end of the array (changing the array dimension from [5][5] to [6][5])?
Aug 30 '10 #7
Hi donbock, thanks for your question.
What I want is the set of 5 numbers entered by the user be appended to the beginning of the array and therefore this is going to change the array dimension from [5][5] to [6][5]
Tell me if I wrong, because the size of the array is going to change is desirable the user determine at runtime the size of the array. To accomplish this I need to declare the array using dynamic memory allocation, if so, I tried but I did not know how to code it.
Aug 30 '10 #8
donbock
2,426 Expert 2GB
Refer to Arrays Revealed for advice on how to dynamically allocate two-dimensional arrays.

Are you sure a two-dimensional array is needed? My inclination would be to define a structure for each set of 5 values, dynamically allocate a structure each time a new set is introduced, and link the structures together:
Expand|Select|Wrap|Line Numbers
  1. struct set {
  2.     struct set *next, *prior;
  3.     int values[5];
  4.     };
(You may not need the prior pointer.)
Aug 30 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Demian | last post by:
Hi, I developed an OCX in VC++, wich takes a VARIANT as parameter and return a Long as the result of the operation. This VARIANT cames from a client VB6.0 wich send an array of Byte as a VARIANT,...
12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
7
by: Eric Boutin | last post by:
Hi ! I would like to generate an array of type char; I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them;...
9
by: sathya | last post by:
I was going through an Boyer-Moore-Horspool pattern match, I saw a array inside a array, like the below, skip ] = patlen - i - 1; The array is decleared as int skip; unsigned char *pat;
1
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float...
0
by: skm376 | last post by:
I am a little confused as to how I need to allocate memory for a dynamic array inside a struct. Here is the struct that I am using: typedef struct command_t *commandPtr; typedef struct...
2
by: Ananas | last post by:
Hi, Please give me an idea how to send a static array from dll written on C++ to C# application. This is a C++ code: const SIGNATURE_LENGTH = 50; struct Info {
1
by: sumitmishra | last post by:
HI FRIENDS i have a problem . i am trying to allocate array inside the for loop . Below is my code here int** character has been allocated inside the for loop and then i have used the delete...
3
by: johnmmcparland | last post by:
Hi all, I would like to have a static constant array inside a class definition which would contain the number of days in each month (I am writing a Date class as an exercise). However my...
4
by: Saile | last post by:
I'm creating an array inside the constructor of my class. Looks like: class SPR_ANIM { public: int index; SPR_ANIM::SPR_ANIM(int num); void Draw(); }; SPR_ANIM::SPR_ANIM(int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.