How to hold the right data into an array
-
Hi Zlatko, I ran the last program you mailed me and the output is not what I want. Could you run the program I will mail you. In this program I'm not using neither the class structure or the shiftDownOneRow() function. Not the class structure because I'm not still familiar with it. Not the shiftDownOneRow() function because in the program it is not working the way I want ( I'll explain you later how I want this function works in the program). This program has two problems: 1) The smallest total = 0; How to output the right smallest total? 2) I added to this program the code of how to output the numbers IN and/or OUT of a matrix set for the user (you will recognize this code because you helped me with it in the past). In this program the line 214 is not working because when the function isInArray() is called it works with the matrix data not with the matrix into the array foundRows[]. Could you check this program to see why this two problems happen. Thanks. const int rows=10; const int columns=5; int wishedNumber; int rowNumber; int columnNumber; using namespace std; void secondPrintHeading(); int getHighestSum( int t[], int n); int getSmallestSum(int t[], int n); int getWishedNumber(); void displayCombination(int [], int); void printHeading(); bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow); int main() { cout bool foundTheNumber = false; char choice; int count = 0; int counting = 0; stringstream inMatrix; stringstream outMatrix; stringstream outputRow; int sizeMatrixInOut; int foundCount=0; int notFoundCount=0; int x = 0; int y = -1; int array[rows][columns] ={ {2, 15, 18, 20, 39}, {7, 11, 14, 15, 34}, {1, 31, 33, 34, 50}, {8, 18, 45, 47, 50}, {1, 10, 12, 32, 36}, {3, 4, 15, 27, 37}, {1, 2, 13, 19, 27}, {12, 17, 21, 23, 30}, {11, 36, 37, 41, 55}, {4, 12, 13, 21, 27 }}; printHeading(); for(int i=0; i cout cout do { cout >rowNumber; do { cout >columnNumber; do { memset(foundList, 0, sizeof(foundList)); memset(foundRows, 0, sizeof(foundRows)); memset(foundSums, 0, sizeof(foundSums)); // memset(foundSums, 0, sizeof(int)*rows); foundTheNumber = false; counting = 0; getWishedNumber(); cout for(int i= 0; i stringstream inMatrix; displayCombination(array[i], columns); cout cout foundSums[i] = sumRow[i]; // For the row that has the wishedNumber in the first column, // examine all the elements in that row and record them in the foundList for(int j = 0; j if(foundTheNumber) { for(int i=0; i stringstream outMatrix; stringstream outputRow; count=0; for(int i = 1; i >sizeMatrixInOut; cout stringstream inMatrix; stringstream outMatrix; stringstream outputRow; // // analyze the row // foundCount=0; notFoundCount=0; x++; y++; for(int j=0; j choice; } while (choice == 'y'|| choice == 'Y'); } // End of the new code if (! foundTheNumber) { cout "column (" } cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); cout } void printHeading() { cout } void displayCombination(int array[], int columns) { for( int i=0; i cout } } int getHighestSum( int t[], int n) { int highest = t[0]; for(int i = 1; i highest) highest = t[i]; return highest; } int getSmallestSum(int t[], int n) { int smallest = t[0]; for(int i = 1; i } int getWishedNumber() { int minWishedNumber = 1; int maxWishedNumber = 56; cout > wishedNumber; while (wishedNumber maxWishedNumber) { cout > wishedNumber; cout return wishedNumber; } bool isInArray(int number, int theArray[][columns], int startingRow, int endingRow) { for(int row = startingRow; row } void secondPrintHeading() { cout Hello Zlatko. With this program I mailed you asking you why the arrays foundSums[rows] and foundRows[57](if I set for(int i=0; i How to hold the right data into an array: 1) Malloc 2) Create a Class 3) Use of the #include 4) The idea and how to write comments about what each section of code is doing 5) The recommendation of the use of a debugger For all this I thank you because using new features of C++ in my program are things that never forget and that is my goal learn this amazing language. I have questions for some code you sent me but first I want to grab a book and try to understand them by myself, meanwhile I did what you told me, I changed the array size to 58 and the error messages went away, but still I am not getting the desire output. The arrays foundSums[rows] and foundRows[57] are still holding zeros, because of this the smallest total always will be zero. Questions: -------------- How can I avoid print out these zeros? I have to create and initialize another bool variable to use it inside the condition if(array[i][columnNumber] == wishedNumber) and this way avoid these zeros?. Once again thanks for your help. ??????????????????????? Hi Zlatko, in the program I mailed you the size of foundSums[] and foundRows[] I set it to 11 just because in the data there are 10 rows, I know is wrong I really don't know their size, I think I did what you told me: int* foundSums = (int*)malloc(sizeof(int)*rows); int* foundRows = (int*)malloc(sizeof(int)*rows); int* sumNumbersInRow = (int*)malloc(sizeof(int)*rows); but the program does not output what I want may be because is the first time I am using Malloc and I am missing something. Could you look at the program to see what is wrong. THANKS. the following program output a matrix data with the sum of each number of a row, later the user choose his own size of a matrix to get from this matrix the rows with a number in a column chosen by him together with the smallest sum, highest sum and the numbers in and/or out of that matrix, later the user enter and output five numbers in the returned matrix together with sum of each row. I created: 1) The array foundSums[] to hold the sums of numbers of each row of a matrix, column and number chosen by the user so this way with the help of the bool foundTheNumber = true get the smallest sum and highest of that matrix. 2) The array foundRows[] to hold the rows of a matrix, column and number chosen by the user so this way with the help of the bool foundTheNumber = true output this rows together with the row enter by the user. It is impossible to choose the real size for this two arrays because theirs sizes are known at run time. May be in this case the only solution is to use Dynamic Allocation of Arrays for this two arrays or may be there is another solution for the arrays foundSums[] and foundRows[] in the program so this way they hold what they have to. Can you help me, once again Thanks. This is the program: int rows=10; const int rowMax=15; const int columns=5; int wishedNumber; int rowNumber; int columnNumber; using namespace std; int getHighestSum( int t[], int n); int getSmallestSum(int t[], int n); int getWishedNumber(); void displayCombination(int [], int); void printHeading(); void shiftDownOneRow(int theArray[][columns]); int main() { cout int array[rowMax][columns] ={ {2, 15, 18, 20, 39}, {7, 11, 14, 15, 34}, {1, 31, 33, 34, 50}, {8, 18, 45, 47, 50}, {1, 10, 12, 32, 36}, {3, 4, 15, 27, 37}, {1, 2, 13, 19, 27}, {12, 17, 21, 23, 30}, {11, 36, 37, 41, 55}, {4, 12, 13, 21, 27 }}; printHeading(); for(int i=0; i cout cout do { cout >rowNumber; do { cout >columnNumber; do { memset(foundSums, 0, sizeof(foundSums)); memset(foundRows, 0, sizeof(foundRows)); memset(userSumNumbersInRow, 0, sizeof(userSumNumbersInRow)); memset(foundList, 0, sizeof(foundList)); foundTheNumber = false; counting = 0; getWishedNumber(); cout for(int i= 0; i stringstream inMatrix; displayCombination(array[i], columns); cout cout foundSums[i]= sumNumbersInRow[i]; for(int j = 0; j if(foundTheNumber) { for(int i=0; i stringstream outMatrix; stringstream outputRow; count=0; for(int i = 1; i shiftDownOneRow(array); cout for(int j=0; j > array[0][j]; // just use 0 for the row userSumNumbersInRow[0] = userSumNumbersInRow[0] + array[0][j]; } cout for(int i=0; i cout cout "column (" } cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); return 0; } void shiftDownOneRow(int theArray[][columns]) { for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow) { for(int col = 0; col } void printHeading() { cout } void displayCombination(int array[], int index) { for( int i=0; i cout } } int getHighestSum( int t[], int n) { int highest = t[0]; for(int i = 1; i highest) highest = t[i]; return highest; } int getSmallestSum(int t[], int n) { int smallest = t[0]; for(int i = 1; i } int getWishedNumber() { int minWishedNumber = 1; int maxWishedNumber = 56; cout > wishedNumber; while (wishedNumber maxWishedNumber) { cout > wishedNumber; cout return wishedNumber; }
-
Answer:
Hi Raul. I assume you want the smallest and largest sum of only the rows that have your wishedNumber because your foundSums[X] is initialized only when wishedNumber is found. For rows that don't have the wishedNumber, foundSums[X] remains 0. Then when scanning foundSums, the 0 is picked up as the minimum. To fix it, you must pass foundRows to getSmallestSum and examine foundSums[X] only if foundRows[X] != 0 Also you don't need to loop like this for(int i=0; i because the getSmallestSum and getHighestSim has its own loop. Do like this: // Declaration int getHighestSum( int t[], int found[], int n); int getSmallestSum( int t[], int found[], int n); // Call if(foundTheNumber) { smallestSum = getSmallestSum(foundSums, foundRows, rows); highestSum = getHighestSum(foundSums, foundRows, rows); cout //Definition And define the functions like this: int getHighestSum( int t[], int found[], int n) { int highest = INT_MIN; for(int i = 0; i highest) highest = t[i]; return highest; } int getSmallestSum(int t[], int found[], int n) { int smallest = INT_MAX; for(int i = 0; i } include if your compiler complains about INT_MAX and INT_MIN. Run your program with inputs 7 0 1 You should get smallest is 62, largest is 149 Your second problem is occurring in the new code // This is the new code do { cout >sizeMatrixInOut; cout The last line I pasted shows that the isInArray is called only when foundRows[i] is non zero. It may be that all foundRows[0 to sizeMatrixInOut are 0, so isInArray is never even called. It is time for you to learn how to use a debugger and step through your code. That is a critical step to learning how to program. Try to watch some videos, or read about your debugger at http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks I think you are using code blocks. Is that right ? Use google to search for videos. Also, try putting these lines if (startingRow /* Construct the array */ Array1D(const char* name, int size) { mName = name; mSize = size; mData = new int[size]; zero(); } ~Array1D() { delete [] mData; } /* Access the array element */ int& operator[](int index) { if (index (int)mSize) { std::cout /* Make the array bigger */ void increse(int sizeIncrease) { int newSize = mSize + sizeIncrease; int* newData = new int[newSize]; memcpy(newData, mData, mSize * sizeof(int)); delete [] mData; mData = newData; mSize = newSize; } /* Initialize the array to all 0. */ void zero() { memset(mData, 0, mSize * sizeof(int)); } /* Return the number of elements in the array */ int size() const { return mSize; } private: std::string mName; int* mData; int mSize; }; int rows=10; const int rowMax=15; const int columns=5; int wishedNumber; int rowNumber; int columnNumber; using namespace std; int getHighestSum(Array1D&, int n); int getSmallestSum(Array1D&, int n); int getWishedNumber(); void displayCombination(int [], int); void printHeading(); void shiftDownOneRow(int theArray[][columns]); int main() { cout Array1D foundSums("foundSums", rows); // Sums for a row, so need only enough to match row count. Array1D foundRows("foundRows", rows); // foundRows, need only enough to match row count. Array1D userSumNumbersInRow("userSumNumbersInRow", 58); Array1D sumNumbersInRow("sumNumbersInRow", 58); Array1D foundList("foundList", 58); //int foundSums[11]; //memset(foundSums, 0, sizeof(foundSums)); //int foundRows[11]; //memset(foundRows, 0, sizeof(foundRows)); //int userSumNumbersInRow[2]; //memset(userSumNumbersInRow, 0, sizeof(userSumNumbersInRow)); //int sumNumbersInRow[rows]; //memset(sumNumbersInRow, 0, sizeof(sumNumbersInRow)); //int foundList[57]; //memset(foundList, 0, sizeof(foundList)); bool foundTheNumber = false; char choice; int count = 0; int counting = 0; stringstream inMatrix; stringstream outMatrix; stringstream outputRow; int array[rowMax][columns] ={ {2, 15, 18, 20, 39}, {7, 11, 14, 15, 34}, {1, 31, 33, 34, 50}, {8, 18, 45, 47, 50}, {1, 10, 12, 32, 36}, {3, 4, 15, 27, 37}, {1, 2, 13, 19, 27}, {12, 17, 21, 23, 30}, {11, 36, 37, 41, 55}, {4, 12, 13, 21, 27 }}; printHeading(); for(int i=0; i do { cout >rowNumber; do { cout >columnNumber; do { foundSums.zero(); foundRows.zero(); userSumNumbersInRow.zero(); foundList.zero(); //memset(foundSums, 0, sizeof(foundSums)); //memset(foundRows, 0, sizeof(foundRows)); //memset(userSumNumbersInRow, 0, sizeof(userSumNumbersInRow)); //memset(foundList, 0, sizeof(foundList)); foundTheNumber = false; counting = 0; getWishedNumber(); cout for(int i= 0; i stringstream inMatrix; displayCombination(array[i], columns); cout cout foundSums[i]= sumNumbersInRow[i]; // ZM changed for(int j = 0; j if(foundTheNumber) { for(int i=0; i stringstream outMatrix; stringstream outputRow; count=0; for(int i = 1; i shiftDownOneRow(array); cout for(int j=0; j > array[0][j]; // just use 0 for the row userSumNumbersInRow[0] = userSumNumbersInRow[0] + array[0][j]; } cout for(int i=0; i cout // This is strange, the matrix array has been shifted down, but foundRows has not // so the two are no longer in sync. // This part may not do what you want, but I'm not sure what you want. The rows // where whishedNumber has been found have already been printed out at line 196 for(int i=1; i cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); return 0; } void shiftDownOneRow(int theArray[][columns]) { for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow) { for(int col = 0; col } void printHeading() { cout } void displayCombination(int array[], int index) { for( int i=0; i } int getHighestSum(Array1D& t, int n) { int highest = t[0]; for(int i = 1; i highest) highest = t[i]; return highest; } int getSmallestSum(Array1D& t, int n) { int smallest = t[0]; for(int i = 1; i } int getWishedNumber() { int minWishedNumber = 1; int maxWishedNumber = 56; cout > wishedNumber; while (wishedNumber maxWishedNumber) { cout > wishedNumber; cout } //////////////////////////////////////////////// Raul, as I examine your program, it think it is becoming clearer. FoundRows Lets be clear on what the purpose of foundRows actually is. The user selects a rowNumber, and the program analyzes the matrix from row 0 to rowNumber-1 (lets call this the sub-matrix), looking for wishedNumber in a particular column. Then it is to print all the rows from the sub-matrix that contain the wished number in the chosen column. The foundRows array is supposed to show if the wishedNumber is found in a particular row and chosen column. Is that correct ? That is how I am understanding the program right now. If that is correct, then foundRows needs to be as large as the number of rows in the original matrix, and it needs only to hold the value of 1 or 0. A 1 means that the row is to be printed, and a 0 means that the rows is not to be printed. You don't print the contents of foundRows directly, you need to print row R of the original matrix, if foundRows[R] has a 1. FoundList It seems to me that the foundList is used to help you print all numbers found in all rows from the sub-matrix where wishedNumber is in the chosen column. Is that correct ? In that case, in the code for(int i = 1; i if (foundList[i] == 0) { outMatrix } the line if (foundList[i] == 0) should read if (foundList[i] == 1) or if (foundList[i] != 0) As I said in my last reply, it is important that you write out comments describing the purposes of variables and parts of code. I have found in my own work that making such comments helps me think about the problem and helps keep me on track to a solution. Keep trying. I will try to get back to you with some code on the weekend. Hello Raul. Yes, there are some problems caused by the malloc, but malloc is not the cause of the strange output after you ask the user for the 5 numbers. I cannot believe that it worked in the past. int foundSums[11]; memset(foundSums, 0, sizeof(foundSums)); can be replaced by int* foundSums = (int*)malloc(sizeof(int)*rows); but the memset will not work as written. The foundSums is now a pointer, and pointers are 4 bytes long, so sizeof(foundSums) is 4, no matter how much memory you allocated. The correct memset is this: memset(foundSums, 0, sizeof(int)*rows); This problem also occurs further down in your code where you do memset on foundSums, foundRows, and userSumNumbersInRow. If those variables are now pointers, instead of arrrays, then their size will be 4 and only four bytes will be zeroed. Now, here is another source of confusion. If you keep foundList as an array of 57, instead using dynamic allocation, then you have to treat foundList differently from all the arrays that you allocate with dynamic allocation. For foundList, the line memset(foundList, 0, sizeof(foundList)); is still good. The big source of bad output is after asking the user for 5 numbers. You have the loop for(int i=1; i { cout /* Construct the array */ Array1D(const char* name, int size) { mName = name; mSize = size; mData = new int[size]; zero(); } ~Array1D() { delete [] mData; } /* Access the array element */ int& operator[](int index) { if (index (int)mSize) { std::cout /* Make the array bigger */ void increse(int sizeIncrease) { int newSize = mSize + sizeIncrease; int* newData = new int[newSize]; memcpy(newData, mData, mSize * sizeof(int)); delete [] mData; mData = newData; mSize = newSize; } /* Initialize the array to all 0. */ void zero() { memset(mData, 0, mSize * sizeof(int)); } /* Return the number of elements in the array */ int size() const { return mSize; } private: std::string mName; int* mData; int mSize; }; int rows=10; const int rowMax=15; const int columns=5; int wishedNumber; int rowNumber; int columnNumber; using namespace std; int getHighestSum(Array1D&, int n); int getSmallestSum(Array1D&, int n); int getWishedNumber(); void displayCombination(int [], int); void printHeading(); void shiftDownOneRow(int theArray[][columns]); int main() { cout Array1D foundSums("foundSums", rows); Array1D foundRows("foundRows", rows); Array1D userSumNumbersInRow("userSumNumbersInRow", rows); Array1D sumNumbersInRow("sumNumbersInRow", rows); Array1D foundList("foundList", 57); //int foundSums[11]; //memset(foundSums, 0, sizeof(foundSums)); //int foundRows[11]; //memset(foundRows, 0, sizeof(foundRows)); //int userSumNumbersInRow[2]; //memset(userSumNumbersInRow, 0, sizeof(userSumNumbersInRow)); //int sumNumbersInRow[rows]; //memset(sumNumbersInRow, 0, sizeof(sumNumbersInRow)); //int foundList[57]; //memset(foundList, 0, sizeof(foundList)); bool foundTheNumber = false; char choice; int count = 0; int counting = 0; stringstream inMatrix; stringstream outMatrix; stringstream outputRow; int array[rowMax][columns] ={ {2, 15, 18, 20, 39}, {7, 11, 14, 15, 34}, {1, 31, 33, 34, 50}, {8, 18, 45, 47, 50}, {1, 10, 12, 32, 36}, {3, 4, 15, 27, 37}, {1, 2, 13, 19, 27}, {12, 17, 21, 23, 30}, {11, 36, 37, 41, 55}, {4, 12, 13, 21, 27 }}; printHeading(); for(int i=0; i do { cout >rowNumber; do { cout >columnNumber; do { foundSums.zero(); foundRows.zero(); userSumNumbersInRow.zero(); foundList.zero(); //memset(foundSums, 0, sizeof(foundSums)); //memset(foundRows, 0, sizeof(foundRows)); //memset(userSumNumbersInRow, 0, sizeof(userSumNumbersInRow)); //memset(foundList, 0, sizeof(foundList)); foundTheNumber = false; counting = 0; getWishedNumber(); cout for(int i= 0; i stringstream inMatrix; displayCombination(array[i], columns); cout cout foundSums[i]= sumNumbersInRow[i]; for(int j = 0; j if(foundTheNumber) { for(int i=0; i stringstream outMatrix; stringstream outputRow; count=0; for(int i = 1; i shiftDownOneRow(array); cout for(int j=0; j > array[0][j]; // just use 0 for the row userSumNumbersInRow[0] = userSumNumbersInRow[0] + array[0][j]; } cout for(int i=0; i cout cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); cout > choice; } while (choice == 'y'|| choice == 'Y'); return 0; } void shiftDownOneRow(int theArray[][columns]) { for(int shiftRow = rows-1; shiftRow >= 0; --shiftRow) { for(int col = 0; col } void printHeading() { cout } void displayCombination(int array[], int index) { for( int i=0; i } int getHighestSum(Array1D& t, int n) { int highest = t[0]; for(int i = 1; i highest) highest = t[i]; return highest; } int getSmallestSum(Array1D& t, int n) { int smallest = t[0]; for(int i = 1; i } int getWishedNumber() { int minWishedNumber = 1; int maxWishedNumber = 56; cout > wishedNumber; while (wishedNumber maxWishedNumber) { cout > wishedNumber; cout } Hello Raul. If you know the array size at runtime only, then the best option is to allocate memory dynamically. The second option is to declare a large array and hope that is will always be large enough for your program. However, with the second option, it is always possible that your estimate will be wrong, and one day your program will crash and you'll have no idea why. Luckily, for single dimension arrays, the solution is very simple. Instead of having int foundSums[11]; Do this: int* foundRows = (int*)malloc(sizeof(int) * 11); Malloc takes a size specification in bytes, so if you want 11 integers, you need to tell malloc that you want sizeof(int) * 11 bytes. An integer is 4 bytes long, so this would allocate 44 bytes, or 11 integers. In your program, this line won't compile int sumNumbersInRow[rows]; because rows is not a constant. You can do the following int* sumNumbersInRow = (int*)malloc(sizeof(int)*rows); For one dimensional arrays allocated with malloc, you access the elements with square brackets the way you are used to, so no other changes are needed. That is the answer to your question. I don't know if you wanted me to look at your program, but I suggest you fix it so it compiles, and then try to run it and debug it if it has problems. Then, if you need more help, let me know. Bye for now. Zlatko
Miningco.com Visit the source
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How do I define the index within the array?Best solution by Stack Overflow
- How to dynamically create object properties from an array in Javascript?Best solution by nfriedly.com
- How can we get SBJSON connectionDidFinishLoading method inside array data in another class?Best solution by stackoverflow.com
- How to hold the double bass?Best solution by playbassnow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.