Question

In: Computer Science

In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry...

In C++, create a 3-by-3 array that is capable of storing integers and initialize each entry to 0. Ask the user to supply a row and a column and a value, then put the value into the array at that row and column. Print the array to the screen as a table with 3 rows and 3 columns, and ask the user for another row, column, and value. Repeat until user inputs values for all entries.

Once finished, compute and print the total of the values in the array.

1. When the array is printed to the screen as a table, use setw() to ensure the numbers have enough room. You may assume the numbers are no more than 3 digits each.

2. Your program needs to check whether the user input row and column are valid. If the user inputs an invalid row or column (i.e. less than 1 or greater than 3), prompt the user to re-input valid row and column.

3. If an entry already has a user input, your program should disallow user to input a value at the same entry and also ask user to enter a value at a different entry.

Solutions

Expert Solution

Please follow all the comments in the code for a better understanding

#include <iostream>
#include <iomanip>  
using namespace std;
//function return 1 if array contains 0 because the input prompt coninues when array has 0's
int check(int arr[3][3]){
    int i,j;
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    if(arr[i][j]==0)
    return 1;//input process to be continued
    
    return 0;//input process to be stopped
}

//fuction returns 1 if value inserted successfully
int setValue(int i,int j,int val,int arr[3][3]){
    if(arr[i][j]!=0)
    return 0;//already assigned 
    
    arr[i][j]= val;
    
    return 1;
}

//print the array elements 
void print(int arr[3][3]){
      int i,j;
    //setw sets offset sapce between elements 
    cout<<setw(3);
    for(i=0;i<3;i++){
    for(j=0;j<3;j++){
    cout<<arr[i][j]<<setw(3);
    }
    cout<<endl;
    }
    
}


int main()
{
    int arr[3][3];
    int i,j,k;
    
    //initialize all values with zeroes in arr
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    arr[i][j]=0;
    
    // continuosly prompt for input until array doesnt contain 0's    
    while(check(arr)){
        //read input values
        int row,col,val;
        cout<<"Row   Col   value\n";
        cin>>row>>col>>val;
        
        //if row and columns values are invalid
        if(row>=0 && row<3 && col>=0 && col<3)
        //check value Already inserted in given position
        if(setValue(row,col,val,arr))
        continue;
        else
        cout<<"Already value assigned !! Try inserting in another position";
        else
        cout<<"Enter valid position";
    }
    
    //print the array
    print(arr);
    
    return 0;
}

Output

feel free to ask doubts in the comment section so that I can modify the answer according to your need

thank you

please support by giving a thumbs up.


Related Solutions

C++ please Create a Stats class whose member data includes an array capable of storing 30...
C++ please Create a Stats class whose member data includes an array capable of storing 30 double data values, and whose member functions include total, average, lowest, and highest functions for returning information about the data to the client program. These are general versions of the same functions you created for Programming Challenge 7, but now they belong to the Stats class, not the application program. In addition to these functions, the Stats class should have a Boolean storeValue function...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT