-
newb needs help again.
I need to to the following and I'm still pretty new to coding. Our teacher has asked us to... Formulate a C++ program solution that does the following. It holds in 2 parallel arrays, the values for the day# in the first array and in the second array the temperature for the corresponding day in the first array. These 2 arrays should only be declared after the size for each array has been specified by the user. Input for each array should come from the user. Lastly, the user should then be allowed to specify the day the user wants to find a temperature value for. In order to do this, the first array should be traversed looking for the matching day element, and from that matching index, find the corresponding temperature in the other array. That temperature value should be printed out. Usage of Loops is an absolute must in this Lab.
I'm unsure how to get started on this seeing the array has to be user defined? Any push in the right direction would be appreciated.
Thanks!
-
You can use dynamic memory allocation
Code:
int userInputValue = 0;
//get user input
int *userSpecifiedArray = new int[userInputValue];
//do operations
delete [] userSpecfiedArray;
Or you could use std::vector, But since this is a school assignment, ussually teachers don't like those even though thats what we would use :D
-
Tank you very much.
Another thing I don't understand is the user will enter in the number of day's they want to view. that would populate the size of one array but what about the array for the temps? How will that array get populated with temperatures? should't the array for temps be the same as what the user specified for the number of days? Where will it get it's values?
-
"Input for each array should come from the user."
-
And since they're parallel arrays - you should make them the same length...
-
so I'm thinking program would ask user to enter in how many cities they want to view. User inputs a number. Then run a loop that has them enter in a temp the number of times they entered in for number of cites. Would that fill in the the 2nd array?
-
OK here's what I have so far. I have it accepting input from the user. But i"m getting lost when it comes to accessing the arrays.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int day = 0;
int temp = 0;
int *numDay = new int[day];
int *temps = new int[temp];
cout << "Enter the amount of cities you would like to view: ";
cin >> day;
for (int x=0; x<day; x++)
{
cout << "Enter a temp: ";
cin >> temp;
}
if (numDay[x] == day)
{
numDay = day[x];
temps = temp[x];
cout<<"day[x]: " <<"temp[x]: ";
}
}
am I anywere in the ballpark?
-
You are close.
Should be:
Create holder city variable
Get user input for city variable
Allocate memory from input variable.
Also. In your loop you want to actually modify the array value for the temp for the current index.
-
You MUST free everything you allocate.
So for every
int* numDay = new int[day];
int* temps = new int[temp];
You must also do
delete [] numDay;
delete [] temps;
Also, you need to think over your design. Pseudo code and flowcharts help.
Like:
numDay = day[x];
temps = temp[x];
But temp is not an array!!
And you haven't even filled anything in the array day yet!!