help much needed on TEMP assingment (ANY FEEDBACK IS GOOD FEEDBACK)

This assignment is pretty confusing just because it has a lot of steps and i'm pretty lost on what i'm doing wrong but i can not seem to get the correct code, any sort of feedback helps since i am new to c++. THANKS FOR ANY INPUT GIVEN

1) Create a array of Fahrenheit temperatures, as follows

int fahr []= {0,1,2,3,4,5,6,7,8,9,10,32,33,34,35,36,37,38,39,40};

2) create a function convert(int fahr) that converts Fahr to Cels, such as you did in class, outside of main(), above it.

3) create a loop, that prints side by side Fahrenheit temperatures from the array above (in a similar way how you printed an array in class) and Celsius temperature by calling function convert(...) in the loop to get a matching Celsius temperature for each Fahrenheit temperature that you print.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
#include <iostream>

using namespace std;

//step 1
float
Convert (float TempFer)
{
  float TempCel;

  TempCel = ((TempFer - 32) * 5) / 9;
  return TempCel;
}
//step 2
int
main ()
{
  int fahr[20] =
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 32, 33, 34, 35, 36, 37, 38, 39, 40 };

  // step 3

  float Convert (float);

  int main (void)
  {
    float TempFer;
    float TempCel;
    cout << "Please enter the temperature in Fahrenheit: ";
    cin >> TempFer;
// function call
    TempCel = Convert (TempFer);
    cout << "\n";
    cout << TempFer << " Fahrenheit = " << TempCel << " Celcius" << endl;

    return 0;
  }
Last edited on
I know step 3 looks pretty funky, and i know how to do while loops but i dont understand calling them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

// 2) create a function convert(int fahr) that converts Fahr to Cels
double convert( int fahr )
{
    return ( fahr - 32 ) * 5.0 / 9.0 ; // note: avoid integer division'
}

int main()
{
    // 1) Create a array of Fahrenheit temperatures
    // note: we never need to modify the values in this array, so array of constants
    const int fahr [] = {0,1,2,3,4,5,6,7,8,9,10,32,33,34,35,36,37,38,39,40};

    // the number of items in the array (let the compiler count it for us)
    // note: the size of the whole array divided by the size of the first element
    const int num_items = sizeof(fahr) / sizeof( fahr[0] ) ; 

    // 3) create a loop, that prints side by side ...
    
    // we now know that there are num_items items in the array
    // the index of the first item in the array is zero,
    // and the index of the last item in the array is num_items-1
    // so we need a loop where the index goes from zero to (num_items-1)
    
    // i know how to do while loops: ok, let us write a while loop
    
    int index = 0 ; // index of the current item; we start with the first item
    
    // note: we come out of this loop when index reaches num_items 
    //       ie. when we have gone past the last item at (num_items-1)
    while( index < num_items ) // loop for index 0, 1, 2 ... (num_items-1)
    {
        // the fahrenheit temperature is the item in the array
        const int fahrenheit_temp = fahr[index] ;

        // get the corresponding celsius temperature by calling the funtion
        const double celsius_temp = convert(fahrenheit_temp) ;

        // print the two temperatures side by side
        // the output can be prettier, but we don't bother about that for this exercise
        std::cout << fahrenheit_temp << " F == " << celsius_temp << " C\n" ;

        ++index ; // get to the next item by incrementing the index
    }
}

http://coliru.stacked-crooked.com/a/1c1eac1d4510b501
Last edited on
If you want to use a while, feel free to do it, but here I suggest you to do a for loop as follow :
1
2
3
4
const int num_items = sizeof(fahr) / sizeof( fahr[0] ) ; 
for(int i(0);i<num_items;++i){
    std::cout<<fahr[i]<<" "<<convert(fahr[i])<<std::endl;
}
Last edited on
A range-based loop http://www.stroustrup.com/C++11FAQ.html#for is the best option here:

1
2
3
4
const int fahr [] = {0,1,2,3,4,5,6,7,8,9,10,32,33,34,35,36,37,38,39,40};

for( int fahrenheit_temp : fahr )
    std::cout << fahrenheit_temp << ' ' << convert(fahrenheit_temp) << '\n' ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// The assignments wonky anyways just throw this at the teacher, still will satisfy what's needed. 
  
#include <iostream>

//step 1
struct Convert
{
float TempFer1(float);
float TempFer2(float);
};
  int main ()
  {
    float Temp1, Temp2, OutputTemp1, OutputTemp2;
    Convert NewFer;
 std::cout <<"Please enter the temperature in Fahrenheit: ";
    std::cin >> Temp1;
// function call
    OutputTemp1 = NewFer.TempFer1(Temp1);
  	OutputTemp2 = NewFer.TempFer2(Temp1);
    std::cout << "\n";
    std::cout<<OutputTemp1 << " Fahrenheit = /n";
  	std::cout<<  " Celcius: " <<OutputTemp2<<std::endl;

    return 0;
  }
  
  float Convert::TempFer1(float Temp1){

  	 float TempCel1;
       TempCel1 = ((1.8 * Temp1) + 32);
 
  	 return TempCel1;
  	}
  	
  	float Convert::TempFer2(float Temp1){
  
  	   	float TempCel2;
         TempCel2 = (((Temp1 - 32) * 5) / 9);

    return TempCel2;
    }
  
Last edited on
Topic archived. No new replies allowed.