HW 2b-Two-Dimensional Array lows a user to input numbers (integers) into a two-d
ID: 3888558 • Letter: H
Question
HW 2b-Two-Dimensional Array lows a user to input numbers (integers) into a two-dimensional array u Write a program that dimensional array named: Declare a numArray The array should be 3 rows x 4 columns (although you can try different sizes) Pass the array to a function named: getData The function should prompt the user to enter values (see output), o Use nested for loops to read in the data, Once the data has been entered, pass the array to a function named: displayArray o Clear the screen before displaying the data. o Use nested for loops to output the data. Form at the numbers so that they are aligned to the right in each column. Space the columns eve nly (see output). o *OUTPUT: y: Enter integers into the 2-Dimensional arra Enter a number 1 Enter a number: 45 Enter a number 765 Enter a number: 6 Enter a number 32 Enter a number: 45 Enter a number: 789 Enter a number: 343 Enter a number 22 Enter a number: 64 Enter a number: 12 Enter a number: 555 Here is the data in the 2-Dimensional array: 1 45 7656 32 45 789 343 22 64 12 555 Press any key to continue/Explanation / Answer
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
void getData(int numArray[3][4])
{
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
string input;
cout<<"Enter a number: ";
std::getline(cin,input);
int num=atoi(input.c_str());
numArray[i][j]=num;
}
}
}
void displayArray(int numArray[3][4]){
cout<<endl;
cout<<"Here is the data in 2-Dimensional array:"<<endl;
cout<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
cout<<" "<<numArray[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
int numArray[3][4];
getData(numArray);
displayArray(numArray);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.