[SOLO] Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a.
b. FunctionaverageHigh:Thisfunctioncalculatesandreturnstheaveragehightemperaturefortheyear.c. FunctionaverageHigh:Thisfunctioncalculatesandreturnstheaveragelowtemperaturefortheyear.d. FunctionindexHighTemp:Thisfunctionreturnstheindexofthehighesthightemperatureinthearray.
e.
FunctionindexLowTemp:thisfunctionreturnstheindexofthelowestlowtemperatureinthearray.(These functions must all have the appropriate parameters.)

## All 54 Replies

The first thing you should do is break the problem down into simple steps, then outline the steps. From there you can translate those steps into C++ code, or a flowchart, or pseudo code, depending on how you learned about the process of programming.

what is a pseudo code???

my teacher doesnt teach me that....

> my teacher doesnt teach me that....
Then you don't need to use it. :D Ed was giving alternatives, not requirements.

how can i make array on a function>????

how can i make array on a function>????

Do you know what a two-dimensional array is? Do you know anything at all about arrays?

``````int main()
{
int array[5][2]; // this is a 2 dimensional array

}``````

If you are having problems with the concepts of arrays then you need to go back and restudy the material in your textbook. Do the problems at end-of-chapters until you get the concepts straight in your head.

a variable is like a box, that you can keep a single value in. say your array is a FLOAT type, and it's name is "temp". Therefore, in it you keep a floating point value of the temperature. and that's it

a simple (1-D) array is like a row of identical boxes, that you can keep a more than one value in, but one value per box. the name is still "temp" but now they have two "elements": "temp[0]" and "temp[1]" this could hold your "average low temperature" and your "average high temperature" respectively

a 2-D array is like many rows of identical boxes, divided in a grid-like pattern of rows and columns... now say you have 12 rows, and each row has 2 columns... the way you would define this array, at the top of your "main()" routine, is simply: `float temp[12][2];` where the first index represents the "row" elements and can have index values from 0-11, while the second index represents the "column" elements , and can have index values of 0 or 1.

thus, temp[0][0] and temp[0][1] would be the average low and average high for the first month (January).

temp[1][0] and temp[1][1] would be the average low and average high for February

temp[2][0] and temp[2][1] would be the average low and average high for March

etc.

the point of this is so you can have variables representing the row and column... example:

``````printf("enter month (1=Jan ... 12=Dec) : \n");
scanf("%d", &month);

if (month>0 && month<13)    // must subtract 1 to get index
cout << "avg high for that month is " << temp[month-1][1]) << endl;
else
cout << "invalid month number." << endl;``````

.

thank you very much....

will it output juct like this??

month highest temp lowest temp
january 45 32

b. FunctionaverageHigh:Thisfunctioncalculatesandreturnstheaveragehightemperaturefortheyear.c. FunctionaverageHigh:Thisfunctioncalculatesandreturnstheaveragelowtemperaturefortheyear.d. FunctionindexHighTemp:Thisfunctionreturnstheindexofthehighesthightemperatureinthearray.
e.
FunctionindexLowTemp:thisfunctionreturnstheindexofthelowestlowtemperatureinthearray.(These functions must all have the appropriate parameters.)

Difficult to believe that any college professor would actually give you instructions that look like that :icon_eek: Where are the spaces between words :?:

>>will it output juct like this??
No -- Its just simple code so illustrate how to use the arrays.

Start out by creating a program with just the functions that your instructor wants. Get that working and compiling without error. Then start filling in the functions one at a time. Do a function, or a few lines of code, compile until there are no errors, then code a little more.

will it output juct like this??

no, you need to figure this out, dude. its basic "cout" statements.

you say you're studying electrical engineering? you better reevaluate your motivations because you're not going to make it through a semester of EE classes with a helpless attitude.

.

i have a code here....

please help me how can i put a label "row"

``````#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
int x,y;
int a;
const int row= 12,seat= 5;
char plane[row][seat];
cout<<"A\tB\tC\tD\tE\tF"<<endl;
{for (y=0;y<=12;y++)
{for(x=0;x<=5;x++)
{	plane[y][x]='*';
cout<<plane[y][x]<<"\t";
}
cout<<endl;
}
}
getch();
return 0;
}``````
``````const int row= 12,seat= 5;
char plane[row][seat];``````

this is wrong. you cant use variables to declare the size of an array like that. try:

``char plane[12][5];``

you can then *access* the array using variables, like youre doing with the "y" as row, and "x" as seat.

i think you're on the right track.

add a new line between lines 10 and 11 that displays the row number.

lines 10 and 11: Reformat the code so that you can easily read it. Your formatting style is terrible. There is an example of how it should look

``````for (y=0;y<=12;y++)
{
for(x=0;x<=5;x++)
{
plane[y][x]='*';
cout<<plane[y][x]<<"\t";
}
cout<<endl;
}``````

Also, where are all the other functions that are in the requirements for your program ?[/edit]

``````const int row= 12,seat= 5;
char plane[row][seat];``````

this is wrong. you cant use variables to declare the size of an array like that. try:

``char plane[12][5];``

you can then *access* the array using variables, like youre doing with the "y" as row, and "x" as seat.

i think you're on the right track.

Sorry, but he did it the correct way. Declared const variables is completly correct. You would have been right had he not used the const keyword.

commented: um, well, yeah.... oh, look, the queen! +3

but i had already run the program and it run....

you mean even if i had value for row and seat

i nid to write array[12][5]?????

no you don't. I was wrong. As Dragon pointed out,

``````const int row= 12,seat= 5;
char plane[row][seat];``````

is correct because you're using the "const" (constant) modifier.

Sorry for the confusion.

your only issue remaining, is printing the correct info to the screen and formatting it, using the "cout" commands. see Dragon's post #13, above, for a good suggestion.

you're doing just fine. keep at it.

.

i have here now my code.....

how can i make the program run.....until all the ' * ' is change to ' X '

``````#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void userInput();
int main()

{
clrscr();
int x,y;
const int row= 12,seat= 5;
char plane[row][seat];
cout<<"\t\tAirplane Seat Assignment"<<endl;
cout<<"\tA\tB\tC\tD\tE\tF"<<endl;
{for (y=1;y<=13;y++)
{   cout<<"Row "<<y<<"\t";
{for(x=0;x<=5;x++)
{  plane[y][x]='*';
cout<<plane[y][x]<<"\t";
}
cout<<endl;
}
}
}
userInput();
cout<<"Congratulations!! You Now Have A Seat...."
<<" Happy Trip!!!";

getch();
return 0;
}
void userInput()
{
int tt,tr,nsa;
char tc;
char A=1;
char B=2;
char C=3;
char D=4;
char E=5;
char F=6;
cout<<"\nEnter TICKET TYPE [1]First Class [2]Economy Class : ";
cin>>tt;
switch(tt)
{
case 1:
{
cout<<"Enter TICKET [1-2]ROW NUMBER : ";
cin>>tt;
{ if((tt>=1)&&(tt<=2))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
} break;
case 2:
{
cout<<"Enter if [1]Non-Smoking Area or [2]Smoking Area : ";
cin>>nsa;
switch(nsa)
{
case 1:
{
cout<<"Enter TICKET [3-7]ROW NUMBER : ";
cin>>tt;
{ if((tt>=3)&&(tt<=7))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
} break;
case 2:
{cout<<"Enter TICKET [8-13]ROW NUMBER : ";
cin>>tt;
{ if((tt>=8)&&(tt<=13))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
} break;

}
} break;
default:
cout<<"INVALID"<<endl;break;
}
}
``````

its really hard to read, since you don't use code-tags, and all the indentation is lost...

but it looks like you're only running through the routine once.

think of this sort of program flow:

``````int main()
{
initialize_seat_array();

do
{
print_seat_matrix();
result = user_input();

} while(result);
}
``````

where the program flow is broken into three main functions.

(1) -- first you initialize the seat array to all asterisks (`*`).

then you enter a do/while loop that

(2) -- prints the seat array as a matrix on the screen, then
(3) -- queries the user to request a seat assignment.

if the user gets the seat assignment, the array element for that seat is set to 'X'. the loop repeats immediately, back to (2), printing the new state of the seat matrix, and (3) querying the user for another seat request. etc. etc.

the do/while loop continues (2) and (3) indefinitely, until the user indicates they have no more seat assignments to request, at which point "result" will equal zero, and the loop will exit, and the program ends.

.

thank you very much jephthah...

i will try it...

and post it to this forum it is running...

thank you very much//....

{
initialize_seat_array(); //what will i write here????

do
{
print_seat_matrix(); // it doesnt accept print...
result = user_input(); //the result is a one X..

} while(result);

i am using borland.....

what will i do.... so that the inputted seat of the user will only be change to X....

please explain clearly... sori i'm just a newbie...

this is not a verbatim example that you should blindly copy.

this is an "OUTLINE", of general functionality, that give you a top-level overview of how the program goal can be accomplished.

you have already written the "user_input" function, for the most part. break out the part that initializes the Array with asterisks and put that in a function such as "initialize_seat_array" so that it is only called once before going into the do/while loop.

then in the do while loop, write a function that prints the array as a matrix to the screen, prior to each time "user_input" is called.

finally, it is considered bad form to PM people with requests for private help. post your questions in the public forum and wait for one or more people to reply with public answers.

how can i print it to matrix?????....

i dont know how to print it...

i dont know how to print array on a matrix....

do i nid to write a function();
or function(int);

here is my new code.....

please identify what is wrong...

``````#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void userInput();
void array();
void print();
void print_seat();
int main()

{
int result=0;
clrscr();
int plane[13][6];
array();
do
{

userInput();
print();
result++;
}
while(result<=78);
cout<<"Congratulations!! You Now Have A Seat...."
<<" Happy Trip!!!";

getch();
return 0;
}

void print()
{
int x,y;
int tt;
char tc;
const int row= 13,seat= 6;
char plane[row][seat];
cout<<"\t\tNew Airplane Seat Assignment"<<endl;
cout<<"\tA\tB\tC\tD\tE\tF"<<endl;
{for (tt=1;tt<=13;tt++)
{   cout<<"Row "<<tt<<"\t";
{for(tc=0;tc<6;tc++)
{  plane[tt][tc]='X';
cout<<plane[tt][tc]<<"\t";
}
cout<<endl;
}
}
}
}

void array()
{int x,y;
const int row= 13,seat= 6;
char plane[row][seat];
cout<<"\t\tAirplane Seat Assignment"<<endl;
cout<<"\tA\tB\tC\tD\tE\tF"<<endl;
{for (y=1;y<=13;y++)
{   cout<<"Row "<<y<<"\t";
{for(x=0;x<6;x++)
{  plane[y][x]='*';
cout<<plane[y][x]<<"\t";
}
cout<<endl;
}
}
}
}

void userInput()
{
const int row= 12,seat= 5;
char plane[row][seat];
int tt,nsa;
char tc;
char A,B,C,D,E,F;
cout<<"\nEnter TICKET TYPE [1]First Class [2]Economy Class : ";
cin>>tt;
switch(tt)
{
case 1:
{
cout<<"Enter TICKET [1-2]ROW NUMBER : ";
cin>>tt;
{ if((tt>=1)&&(tt<=2))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
plane[tt][tc]='X';
} break;
case 2:
{
cout<<"Enter if [1]Non-Smoking Area or [2]Smoking Area : ";
cin>>nsa;
switch(nsa)
{
case 1:
{
cout<<"Enter TICKET [3-7]ROW NUMBER : ";
cin>>tt;
{ if((tt>=3)&&(tt<=7))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
} break;
case 2:
{cout<<"Enter TICKET [8-13]ROW NUMBER : ";
cin>>tt;
{ if((tt>=8)&&(tt<=13))
cout<<"Row Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
cout<<"Note : Letter is CASE SENSITIVE!!!"<<endl;
cout<<"Enter TICKET [A-F]COLUMN LETTER : ";
cin>>tc;
{ if ((tc>='A')&&(tc<='F'))
cout<<"Column Accepted"<<endl<<endl;
else
cout<<"Invalid"<<endl<<endl;
}
} break;

}
} break;
default:
cout<<"INVALID"<<endl;break;
}
}
``````

Okay, here's a slight rework of your program.
It does the bare bones initializing of the plane
array and allows you to display the plane as many
times as you want. Until you understand what's
being done, and why, you shouldn't do one line
more than this.

``````#include<iostream>

using namespace std;

const int ROWS = 13;
const int SEATS = 6;

void initializePlane(char **);
void printPlane(char **);

int main()
{

bool result = true;
int plane[ROWS][SEATS];

intializePlane(plane);

char ch;

do
{
printPlane(plane);
cout << "do again y/n";
cin >> ch;
if(ch != 'y')
result = false;
}while(result);

getch();
return 0;
}

void initializePlane(char ** plane)
{
for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < SEATS; y++)
{
plane[x][y] = '*';
}
}

void printPlane(char ** plane)
{
cout<<"\t\t Airplane Seat Assignment"<<endl;

for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < SEATS; y++)
{
cout << plane[x][y];

}
cout<<endl;
}
}``````

what will i write on the **?????

will i write there my array???

what will i write on the **?????

Nothing. The above example is working code, try it out. If you're wondering what the '**' are for, click here to learn about pointers.

Also please read this and this about using code-tags. It makes your code readable for other people :)

i am using borland c++ and it doesnt run....

and i tried it also to visual c++ and it did not run...

it has failed...

i have a borland compiler....

what codes will i change...

using borland it has 15 errors...

``````------ Build started: Project: COE050, Configuration: Debug Win32 ------
Compiling...
new array.cpp
c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(17) : error C3861: 'intializePlane': identifier not found

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(23) : error C2664: 'printPlane' : cannot convert parameter 1 from 'int [13][6]' to 'char **'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(30) : error C3861: 'getch': identifier not found

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(45) : error C2601: 'printPlane' : local function definitions are illegal

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(35): this line contains a '{' which has not yet been matched

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(58) : fatal error C1075: end of file found before the left brace '{' at

'c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\new array.cpp(35)' was matched
planesas.cpp

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\planesas.cpp(46) : error C2228: left of '.reserveSmoking' must have class/struct/union
type is 'int'

c:\users\ethelbert karl\visual studio 2008\projects\coe050\coe050\planesas.cpp(46) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Generating Code...
Build log was saved at "file://c:\Users\Ethelbert Karl\Visual Studio 2008\Projects\COE050\COE050\Debug\BuildLog.htm"
COE050 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
``````

this are the errors i encounter using the compiler microsoft visual c++

Ok. I apologize, there are a few bugs in lerner's code.
I'm not going to name them all, but here's the improved version:

``````#include<iostream>

using namespace std;

const int ROWS = 13;
const int SEATS = 6;

void initializePlane(char[ROWS][SEATS]);
void printPlane(char[ROWS][SEATS]);

int main()
{

bool result = true;
char plane[ROWS][SEATS];

initializePlane(plane);

char ch;

do
{
printPlane(plane);
cout << "do again y/n";
cin >> ch;
if(ch != 'y')
result = false;
}while(result);

cin.get();
return 0;
}

void initializePlane(char  plane[ROWS][SEATS])
{
for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < SEATS; y++)
{
plane[x][y] = '*';
}
}
}

void printPlane(char  plane[ROWS][SEATS])
{
cout<<"\t\t Airplane Seat Assignment"<<endl;

for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < SEATS; y++)
{
cout << plane[x][y];

}
cout<<endl;
}
}``````

@ Lerner: Long night? I've never caught you on an error before ;)

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.