I am attempting to complete a homework assignment. The progam basically requires input from the keyboard into an array and then to perform some calculations on the input information. I believe I can do the calculations, but I cannot figure out how to get the input into the array. The assignment requires a two-dimensional array. My book, all examples from the instructor, everything on the internet ( I have tried everything I know) initializes the array with static values. I cannot figure out how to make the input data go into the array to be manipulated. Please provide me with a direction to go I am stumped. I have attached what I have so far.
talk2tisa 13 Newbie Poster
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int rowSize=2;
const int colSize=12;
void getData (int data[][colSize], int rowSize);
void pause ();
int main ()
{
int temp;
int a;
cout<<"Enter high temperature for each month"<<endl;
cin>>temp;
int temps [rowSize][colSize]= { {temp};
cout<<"Enter low temperature for each month"<<endl;
cin>>temp;
int temps [rowSize][colSize]= {temp};
getData(temps, rowSize);
cout<<endl;
pause ();
return 0;
}
void getData(int data[][12], int rowSize)
{
int row;
int col;
for (row = 0; row < rowSize; row++)
{
for (col = 0; col < 12; col++)
cout<< data[row][col] << " ";
cout << data[row][col];
}
}
void pause()
{
cout << endl;
cout << "Enter a character to quit --> "; //
char dummy;
cin >> dummy; // Wait
}
Salem 5,199 Posting Sage
In a loop which runs 12 times, you want
cin >> temps[0][col]; // this is max
cin >> temps[1][col]; // this is min
talk2tisa 13 Newbie Poster
Thanks for the direction.
Ok, I have attached my new stab at it. Let me note that I don't really have to ouput the array, I just put that in so I could see what was going on. However, I still don't think it's quite right because this is what I get:
Enter high temperature for each month
1 2 3 4 5 6 1 2 3 4 5 6 (my entry)
2 (output)
3
4
5
6
1
2
3
4
5
6
1 2 3 4 5 6 1 2 3 4 5 6 (my entry, no prompt)
1 (output)
2
Enter low temperatures for each month
4
5
6
1
2
3
4
5
6.
Obviously, I want all the numbers to appear and then I want the prompt for the next set of numbers. It seems that my prompt is being made part of the array, but I can't figure out how to fix it. And where is the number 1 from the first array? Help!
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int rowSize=2;
const int colSize=12;
int main ()
{
int temps [rowSize][colSize];
int temp1;
int temp2;
int rows;
int cols;
cout<<"Enter high temperature for each month"<<endl;
cin>>temps[0][cols];
for (int rows=0; rows<1; rows++)
for (int cols =0; cols<=12; cols++)
{
cin>>temps[0][cols];
cout<<temps[0][cols];
cout<<endl;
}
cout<<"Enter low temperatures for each month"<<endl;
cin>>temps[1][cols];
for (int rows=0; rows<1; rows++)
for (int cols =0; cols<12; cols++)
{
cin>>temps[1][cols];
cout<<temps[1][cols];
cout<<endl;
}
cout << "Press q and then Enter to quit --> "; //
char dummy;
cin >> dummy;
return 0;
}
iamthwee
It's fairly simple use a separate for loop for both input and output:-
i.e
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int rowSize = 2;
const int colSize = 12;
int main()
{
int temps [rowSize][colSize];
int temp1;
int temp2;
int rows;
int cols;
cout << "Enter high temperature for each month" << endl;
//for (int rows=0; rows<1; rows++)
for ( int cols = 0; cols <= 12; cols++ )
{
cin >> temps[0][cols];
}
//print em
for ( int cols = 0; cols <= 12; cols++ )
{
cout << temps[0][cols] << " ";
}
cin.get();
cin.get();
}
talk2tisa 13 Newbie Poster
Ok,
I am making progress. Now my issue is that when I try to average, I get the average of the second set of numbers for both high and low temperatures. I can't determine how to separate the two. A little assistance please?
Thanks,
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int numberOfRows = 2;
const int numberOfColumns = 12;
void getData(int matrix[][numberOfColumns], int noOfRows);
void avgHigh(int matrix[][numberOfColumns], int noOfRows);
void avgLow(int matrix[][numberOfColumns], int noOfRows);
void pause ( );
int main ()
{
int temps[numberOfRows][numberOfColumns];
getData(temps, numberOfRows);
avgHigh(temps, numberOfRows);
cout << endl;
avgLow(temps, numberOfRows);
cout << endl;
pause ( );
return 0;
}
void getData(int matrix[][12], int rowSize)
{
cout<<"Enter high temperature for each month"<<endl; ;
for (int rows=0; rows<1; rows++)
for (int cols =0; cols<12; cols++)
{
cin>>matrix[rows][cols];
cout<<matrix[rows][cols];
cout<<endl;
}
cout<<"Enter low temperatures for each month"<<endl;
for (int rows=0; rows<1; rows++)
for (int cols =0; cols<12; cols++)
{
cin>>matrix[rows][cols];
cout<<matrix[rows][cols];
cout<<endl;
}
}
void avgHigh(int matrix[0][12], int rowSize)
{
int rows, cols;
int sum;
int average;
for (rows = 0; rows < 1; rows++)
{
sum = 0;
for (cols = 0; cols < 12; cols++)
sum = sum + matrix[rows][cols];
average=sum/12;
cout << "Average high temperature: " << average<<endl;
}
}
void avgLow(int matrix[1][12], int rowSize)
{
int rows, cols;
int sum;
int average;
for (rows = 0; rows < 1; rows++)
{
sum = 0;
for (cols = 0; cols < 12; cols++)
sum = sum + matrix[rows][cols];
average=sum/12;
cout << "Average low temperature: " << average<<endl;
}
}
void pause()
{
cout << endl;
cout << "Enter a character to quit --> "; //
char dummy;
cin >> dummy; // Wait
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.