I have question in c++ program I need the answer as soon as possible I attached the file Loops often have loops inside them. These are called “nested” loops. In

I have question in c++ program I need the answer as soon as possible I attached the file Loops often have loops inside them. These are called “nested” loops. In this exercise you will finish a nested loop that reads lines of numbers from a file and computes and prints out the sum of each line. //C++ Lab3C.cpp // // // //1. [include required header files]// using namespace std; int main() { [2. Declare required variables] try { //3> put your file name & Open file as an input mode ifstream Openfile(” “); //4> If file doesn’t exist then throw error number if(Openfile.good()) { while( getline(Openfile, sLine)) //Outer While Loop Condition { cout << "The Contents of line sLine" << sLine << "\n"; stringstream Str(sLine); while (Str >> temp ) //Inner While Loop Condition { cout << "String ~to double" << temp; }// Inner while Loop } //Outer While Loop Openfile.close(); //3> Catch the error number and display message } else { throw 10; } } //if catch(int e) { cout << "File Not found " << e << endl; } //system("pause"); //Pause return 0; }//main 10 20 30 40 50 60 70 80 90.0 11 13.0 20 40 70 19.0 Read Line 0 10.0 Sum 10.0 Read Line 0 20.0 Sum 30.0 Read Line 0 30.0 Sum 60.0 Read Line 0 40.0 Sum 100.0 Read Line 0 50.0 Sum 150.0 ….. Read Line 3 20.0 Sum 20.0 Read Line 3 40.0 Sum 60.0 Read Line 3 70.0  Sum 130.0 Read Line 3 19.0 Sum 149.0 In C++, to make a 2D array, we simply declare an array where the type is an array. To see how this works, first look at how we create an int array with 10 elements. One might type int arr1D[ 10] ; the above code actually does is create a new “int Array” that reserves enough space to store 10 integers. For creating a 2D arrays, we can make an array of them, by appending another set of square brackets: int arr2D[3][7]; This declares a two dimensional array of ints with 3 rows and 7 columns. By convention, we think of the array as being indexed in “Row Major Order”, which means that the row (y value) comes before the column (x value). Thus, visually, the array looks like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The top left element is at position [0][0], and the bottom right element is at position [2][6]. The array elements are initialized just as in the 1D case ( Numerics are set to 0, and object types are set to null). To access one of the elements of a 2D array, we use double bracket notation again, so we can write arr2D[0][1] = 1; arr2D[2][3] = 4; This would give us the array: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 If we want to visit or process every element of a 2D array, we need a nested for loop to do it. Generally, when you iterate over a 2D array, you want to do it in row major order, because this is more memory efficient. The reason why is that adjacent elements in a row are stored contiguously (one after another) in memory. Another reason, for an English speaker, is that this is the same order your read a book—top to bottom, left to right. The following code iterates over arr2D, changing each value to 1: const int ROW =3; const int COL =7; int arr2D[3][7]; for(int r=0; r < ROW ; r ++) { for(int c =0; COL; r++) { arr2D[r][c] = 1; } } I'm using the variables "r" and "c" because they iterate of the rows and columns of the array, respectively. The outer loop iterates over all the rows, and the inner loop iterates over all the columns in a single row. Notice how the row index (r) comes before the column index (c). Also, notice how the loop bounds are handled in the interior loop. While the outer loop goes from 0 to ROW, the inner loop goes from 0 to COL. , and finish the following program, which computes and prints an addition table using nested loops and a 2D array: //C++ Lab3B.cpp // // // #include using namespace std; typedef int* IntArrayPtr; int main( ) { //Propt user for input cout << " Enter your Size of An Array \n"; //Read an integer from the keyboard & save it to the int variable. //[1. Add Code Here ] //[2. Create one-dimensional dynamic array] //[3. Create two-dimensional dynamic array] //Fill out the table using a netsted loop so that //Table[r][c] = r + c; //[4. Add Code Here] //Itrate over the table, printing each value //so that columns align(hint: use a nested loop ) //[5. Add Code Here] //[6. Delete dynamic array] system("pause"); return 0; }//End of main When finished, with an input value of 4, the program should print something like: 0 1 2 3 1 2 3 4 2 3 4 5 3 4 5 6

Do you need us to help you on this or any other assignment?


Make an Order Now