Portfolio Title





 

 

Student Portfolio >> Index >> Coding >> Assignment

Question 1 
// This program enables the user to enter temperatures for 31 days of January and 
// prints the average of the temperatures over 30, between 20 and 30 and below 20 
//degrees 
  
#include <iostream.h> 
#include <conio.h> 
  
void main() 
{ 
float sumo30=0; // sum of numbers above 30 
int numo30=0; // amount of num above 30 
float sumu30=0; // sum of temp between 20 and 30 
int numu30=0; // amount of temp between 20 and 30 
float sumu20=0; // sum of temp above u 20 
int numu20=0; // sum of temp 20 
for(int i=1;i<32;i++) // loop from 1 to 31, (31 days) 
{ 
float temp; 
cout << "Enter temperature for day " << i << ":"; 
cin >> temp; // input of temp, no restrictions, 
if(temp<20) 
{ 
sumu20+=temp; // sum temp 
numu20++; // increase count 
} 
else if(temp<=30) 
{ 
sumu30+=temp; 
numu30++; 
} 
else 
{ 
sumo30+=temp; 
numo30++; 
} 
} 
float mo30; 
float mu30; 
float mu20; 
if(numo30) 
{ 
mo30=sumo30/numo30; 
} 
if(numu30) 
{ 
mu30=sumu30/numu30; 
} 
if(numu20) 
{ 
mu20=sumu20/numu20; 
} 
float ma=(sumo30+sumu30+sumu20)/(numo30+numu30+numu20); 
cout << "Average of temps over 30: " << mo30 << "\n"; 
cout << "Average of temps over 20 and under 30: " << mu30 << "\n"; 
cout << "Average of temps under 20: " << mu20 << "\n"; 
cout << "Average of all temps: " << ma << "\n"; 
cout << "press enter"; 
getch(); 
} 
  
  
Question 2 
// Calculates the fuel consumption of cars and states whether it's a good, fair or poor 
// consumption 
  
#include <iostream.h> 
#include <conio.h> 
  
void main() 
{ 
int goodc=0; 
int fairc=0; 
int poorc=0; 
float input; 
cout << "Please enter fuel consumption: "; 
cin >> input; 
while(input>0) 
{ 
cout << "Please enter fuel consumption: "; 
cin >> input; 
if(input<7) 
{ 
goodc++; 
} 
else if(input>10) 
{ 
poorc++; 
} 
else 
{ 
fairc++; 
} 
} 
cout << "there are " << goodc << " cars with good consumption\n"; 
cout << fairc << " cars with fair consumption\n"; 
cout << "and " << poorc << " cars with poor consumption\n"; 
getch(); 
} 
  
  
  
  
  
  
Question 2 – TRACE TABLE 
  

input

goodc

fairc

poorc

Input >0

Input <7

Input>10

output

6.3

0

0

0

T

T

F

There are 2 cars with good consumption, 4 cars with fair consumption and 2 cars with poor consumption

10

1

1

0

T

F

T

7.0

1

2

0

T

T

F

7.1

1

3

0

T

T

F

8.6

1

4

0

T

F

T

12.3

1

4

1

T

F

T

11.2

1

4

2

T

F

T

6.4

2

4

2

T

T

F

-8

2

4

2

F

F

F

 

 

 

 

 

 

 

  
  
  
Question 3 
// reads in the grades for 10 students (between 0 ..100) and outputs the average of the 
// students that have passed/failed 
  
#include <iostream.h> 
#include <conio.h> 
  
void main() 
{ 
int sumg=0; 
int numg=0; 
int sumb=0; 
int numb=0; 
for(int i=1;i<11;i++) 
{ 
int temp; 
do 
{ 
cout << "Students grade " << i << " (0...100): "; 
cin >> temp; 
} while(temp<0 || temp>100); 
if(temp>50) // assumed that above 50 means passed 
{ 
sumg+=temp; 
numg++; 
} 
else 
{ 
sumb+=temp; 
numb++; 
} 
} 
float mg=0; 
float mb=0; 
if(numg) 
{ 
mg=float(sumg)/float(numg); 
} 
if(numb) 
{ 
mb=sumb/numb; 
} 
cout << numg << " Students with an average of " << mg << " have passed\n"; 
cout << numb << " Students with an average of " << mb << " have failed\n"; 
getch(); 
} 
<< Back                                                                                                                                                                                        Forward >> 

Coding | Website Scripting | Visual Design | Instructional Design | Website Design | Website Design Tools

 

Copyright 2005 Kerstin Kegel