How to change array elements?

An array inside a 2d array

  • Hi Zlatko, I added some code to the program in topic " An array inside a 2D array ". After the user enter five numbers and this numbers are added to the beginning of a matrix, I wanted to know if this five numbers match with the numbers inside the matrix or not. To do that I use an idea you taught in other topic. I created an array to hold the array that is initialized in the program with a size ( 5 x 5 ) and compare it with the numbers entered by the user. The problem is when the user enter the second row of numbers the size of the matrix becomes in ( 6 x 5 ) and what I want is the size of the matrix to compare with the numbers enter by the user always be ( 5 x 5 ) This the code a go so far: const int maxRows = 10; // This is the maximum number of rows int rows=5; // this is the number of rows present const int columns=5; bool foundTheNumber = false; using namespace std; void printHeading(); void shiftDownOneRow(int theArray[][columns]) { for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow) { for(int col = 0; col } int main() { cout int foundList[57]; memset(foundList, 0, sizeof(foundList)); bool foundTheNumber = false; printHeading(); for(int i=0; i cout >array[i][j]; } } cout choice; } else { break; // exit the loop } } while (choice == 'y'|| choice == 'Y'); cout } void printHeading() { cout cout } THIS IS THE OUTPUT OF THIS PROGRAM: ======================= ROWS (0) (1) (2) (3) (4) ------------------------------ 0) 6 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 5 numbers: 1 2 3 4 5 ===================== ROWS (0) (1) (2) (3) (4) -------------------------- 0) 1 2 3 4 5 1) 6 13 23 45 50 2) 5 12 34 36 51 3) 2 23 25 46 54 4) 2 7 35 39 48 5) 3 5 26 39 49 1 NOT in matrix 2 IN the matrix 3 IN the matrix 4 NOT in matrix 5 IN the matrix 3 numbers IN matrix 2 numbers NOT in matrix 0) 1 2 3 4 5 1) 6 13 23 45 50 2) 5 12 34 36 51 3) 2 23 25 46 54 4) 2 7 35 39 48 5) 3 5 26 39 49 // This is the line I do not need because I // want to keep the size of the matrix ( 5 X 5 ) Would you like to try again? (y/n): Hi Zlatko, I am learning by myself this amazing programming language C++ and I would like to know the different ways to resolve a problem, that way I can understand better this programming language. Tell me if I wrong. I am thinking that your first option I can accomplish it if in the code I mailed you like you said, I shift all the elements down first and after that the user enter the number of rows, but my doubt is how to shift all the elements down. I would like to do your second option too because as you said is the best option and besides it will be the first time I use the concept of dynamic memory allocate and the data structure class so of course, I will appreciate all your help. THANKS. Hi Zlatko, 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. This is the code I got so far. Thanks. 1. #include? 2. #include? 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 >array[i][j]; 41. ????????????} 42. ????????} 43. ????????cout >?choice; 56. ? 57. ????}??while?(choice?==?'y'||?choice?==?'Y'); 58. ????cout<<endl; 59. ????return?0; 60. } 61. ? 62. void?printHeading() 63. { 64. ????cout<<setw(82)<<"================================== "; 65. ????cout<<setw(76)<<"ROWS?????(0)?????(1)?????(2)?????(3)?????(4) "; 66. ????cout<<setw(81)<<"------------------------------------ "; 67. } 68. ?

  • Answer:

    Hello Raul. If you don't need to keep the last row, then you can go back to an array of 5 rows as you had in your original code. You can get rid of the maxRows idea. Just change the shifting code to shift numbers from the second last row to the last row, and then go backwards that way until it shifts numbers from the first row to the second. Then the first row will be available to hold new numbers. void shiftDownOneRow(int theArray[][columns]) { /* start at second last row, and shift it to the last then keep going backwards until the first row is shifted to the second row */ for(int shiftRow = rows-2; shiftRow >= 0; --shiftRow) { for(int col = 0; col } Your next objective is to see if the new numbers match numbers in any of the other rows. The foundList idea here is not so good because as the user enters rows, you really have no control of what values the user enters. If the user enters 100, or 1000, the foundList would have to accommodate that, but your foundList only goes to 57. The foundList idea is a fast method but not flexible. I suggest you go with a slower, but more flexible method of having a function to check if a number is in the array. I think you don't want to check the first row, so we can make the function flexible by specifying which row range to search. /* Search an array from the startingRow to the endingRow for a number. Return true if the number is found, and false otherwise bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow) { for(int row = startingRow; row } Perhaps I misunderstood, and you want to search the original matrix, before inserting the 5 new numbers, if so, then do this: 1) place the 5 original numbers into a temporary array 2) search the original matrix 3) shift down 5) insert the 5 numbers into the matrix Anyway, here is a program that does something. Maybe it does what you want, but if not, I'm sure you can change it. I have commented-out all the code that uses the foundList. const int rows=5; // this is the number of rows present const int columns=5; bool foundTheNumber = false; using namespace std; void printHeading(); /* start at second last row, and shift it to the last then keep going backwards until the first row is shifted to the second row void shiftDownOneRow(int theArray[][columns]) { for(int shiftRow = rows-2; shiftRow >= 0; --shiftRow) { for(int col = 0; col } /* Search an array from the startingRow to the endingRow for a number. Return true if the number is found, and false otherwise bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow) { for(int row = startingRow; row } int main() { cout //int foundList[57]; //memset(foundList, 0, sizeof(foundList)); //bool foundTheNumber = false; printHeading(); for(int i=0; i do { // shift down the rows of the array shiftDownOneRow(array); cout >array[0][j]; // just use 0 for the row } //} cout cout choice; } while (choice == 'y'|| choice == 'Y'); cout } void printHeading() { cout } Edit September 2, 2010 Raul, I've created a google site at https://sites.google.com/site/zlatkoscodingtopics/ as a place for more detailed answers. I'll be adding to it in the next few days. Today it's just a title page. Ok, Raul, lets start with the simple option. The code below is your original code with a few small changes. I have made comments where I have made changes. It shows you how to shift down the elements of the array. I will add to this answer later with the second option. You will get another email when I add to the answer. You can ask more questions about the code below if you like. const int maxRows = 7; // This is the maximum number of rows int rows=5; // this is the number of rows present const int columns=5; using namespace std; void printHeading(); /* function shiftDownOneRow takes a 2D array and shifts all elements down one row, leaving the first row available for new data. The first row is not touched. Notice that the shifting has to be done form last row to first row. The function uses the global variables rows and columns. It increments the rows variable after the shifting is done The function assumes that the array has space, so the caller should check that rows for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow) { for(int col = 0; col } int main() { cout printHeading(); for(int i=0; i // shift down the rows of the array shiftDownOneRow(array); cout for(int i=0; i >array[i][j]; } } cout // don't allow array to overflow if (rows > choice; } else { break; // exit the loop } } while (choice == 'y'|| choice == 'Y'); cout } void printHeading() { cout } Hello Raul You have two options here. One option is to create your 2D array with as many rows as you will need during the run of the program. So, create the array with 6 or more rows and keep track of how many rows are filled with data. The programming for this is simple, but not flexible, because your array cannot grow beyond the preset bounds. Another option is to allocate memory dynamically. This allows the array size to change at runtime. If the array grows many times during the run of the program, then it is common to allocate more rows than needed, so that time is not wasted in the memory manager. Whenever the program reallocates memory, it would have to copy elements from the original memory to the new memory. The programming for this is more complicated, especially because you lose the 2 dimensional quality of the array, and you need to calculate where in memory an element is based on the 2 indexes and the array width. For example, if you have an integer pointer int *p; representing the 2D array with R rows and C columns, and you want element [i][j], that element will be located at p + i*C+j If choosing this option, it is good to encapsulate the array into an array class, and overload the [] operator. That allows your program to keep using the array[i][j] syntax. Such a class would contain the following data: int* pArray; // pointer to the memory int rowCount; // count of the number of rows in the array int colCount; // count of the number of columns in the array And the following functions to access elements and resize the array int& elementAt(int i, int j) { return *(pArray + i * colCount + j); } void resize(int newRowCount); // resizes the array Overriding operator[] is more complicated, so I suggest you use the elementAt access function for now. I could help you build such a class if you like. Regardless of which option you choose, if you want to add elements to the start of the array, you will need to first shift all other elements down. I hope that helps you out.

Miningco.com Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.