Arrays

Hi,

I am a C++ learner , therefore, please do not judge me very much for not handling such a simple problem. I am trying to write a code (playing), which converts Fahrenheits into Celcius, and prints out both results on the screen. Like

F[0] = "", C=[0] = ""

and like that. But I cannot figure out how to make the program remember, print out the Fahreneheits as well. All I can successfully do is to print out only Celcius, using an array.

C[0] = " "

Can you please tell me how to handle this problem? Do I need to make, apparently I need, a multidimensional array?? My code is appended below.

Thanks!

// arrays as parameters
#include <iostream>
using namespace std;

const int MAX = 5;

int main ()
{

int array[MAX][MAX];
int i;
int j;

for (i=0; i < MAX; i++)

for (j=0; j < 1; j++)
{

int C, F;

cout << "Enter temperature in Fahrenheits:";
cin >> F;

C = (F - 32) * 5/9;
array[i][j] = C,F;

}

for (i = 0; i <= 4; i++)
for (j = 0; j <= 4; j++)

cout << "C[" << i << " " << j << "] = " << array[i][j] << endl; //Print out data into the screen

return 0;
}
closed account (S6k9GNh0)
I'm confused. You don't need arrays at all, none the less a multi-dimensional array.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>

double ctof(double degree)
{
   return  degree * 9/5 + 32;
}

int main(int argc, char ** argv)
{
   if (argc < 2)
      std::cout << "Usage: ctof <number>" << std::endl;
   else
   {
        std::stringstream ss ( argv[1] );  
        double result = 0;
        ss >> result;   
        std::cout << ctof(result) << std::endl;
   }
   return 0;
}
@ computerquip: I think he's trying to generate a table of converted values.

@ OP: No offense but you've bitten off more than you can chew. Start with the basics.

http://cplusplus.com/doc/tutorial/
That is right. I am trying to make a table of converted values. I can do it only for one type of values. Here is an example for conversion of Fahrenheit to Celsius, but I do not see the Fahrenheit values, only Celsius.

// Program converts temperature from Fahrenheits into Celcius
// and writes the values into a file
//Created 05/05/2010
//by Kirill Pushkin

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

const int MAX = 10;

int main ()

{
int temperature[MAX]; // array
int i;

for (i = 0; i < MAX; i++)

{
int celcius, fahrenheit;

cout << "Enter temperature in fahrenheits:\n>";
cin >> fahrenheit;

celcius = (fahrenheit - 32) * 5/9; // conversion of temperature from Fahrenheits into Celcius
temperature[i] = celcius;

}

ofstream myfile; // command to open a file for writing with a name whatever you like
myfile.open ("/home/kirillpushkin/Documents/rootanalysis/C++_progs/examples/temperature.txt"); //creation of a file in a particular directory

for (i = 0; i <= 9; i++)
// Print temperature on the screen

// cout << "temperature[" << i << "] = " << temperature[i] << endl; //Print out data into the screen

myfile << "temperature[" << i << "] = " << temperature[i] << endl; //Writes data directly into a file

myfile.close(); // close the file

return 0;
}

The output:
ubuntu:~/Documents/rootanalysis/C++_progs/examples> cat temperature.txt
temperature[0] = 32
temperature[1] = 26
temperature[2] = 21
temperature[3] = 15
temperature[4] = 10
temperature[5] = 4
temperature[6] = -1
temperature[7] = -6
temperature[8] = -12
temperature[9] = -17

So I made two arrays, but the output of this code is all messed up. Can anybody tell me where I screwed up?
Thanks!

// arrays as parameters
#include <iostream>
using namespace std;

const int MAX = 5;

int main ()
{
int carray[MAX];
int farray[MAX];
int i,j;
int C, F;

for (i=0; i < MAX; i++)

for (j=0; j < MAX; j++)

{

cout << "Enter temperature in Fahrenheits: \n";
cin >> F;
farray[i] = F;

C = (F - 32) * 5/9;
carray[j] = C;

}

for (i = 0; i <= MAX; i++)


for (j = 0; j <= MAX; j++)

cout << "Fahrenheit[" << i << "] = " << farray[i]<< " " "Celsius[" << j << "] = " << carray[j] << endl; //Print out data into the screen

return 0;
}

Fahrenheit[0] = 5 Celsius[0] = -6
Fahrenheit[0] = 5 Celsius[1] = -5
Fahrenheit[0] = 5 Celsius[2] = -5
Fahrenheit[0] = 5 Celsius[3] = -4
Fahrenheit[0] = 5 Celsius[4] = -3
Fahrenheit[0] = 5 Celsius[5] = 10536
Fahrenheit[1] = 10 Celsius[0] = -6
Fahrenheit[1] = 10 Celsius[1] = -5
Fahrenheit[1] = 10 Celsius[2] = -5
Fahrenheit[1] = 10 Celsius[3] = -4
Fahrenheit[1] = 10 Celsius[4] = -3
Fahrenheit[1] = 10 Celsius[5] = 10536
Fahrenheit[2] = 15 Celsius[0] = -6
Fahrenheit[2] = 15 Celsius[1] = -5
Fahrenheit[2] = 15 Celsius[2] = -5
Fahrenheit[2] = 15 Celsius[3] = -4
Fahrenheit[2] = 15 Celsius[4] = -3
Fahrenheit[2] = 15 Celsius[5] = 10536
Fahrenheit[3] = 20 Celsius[0] = -6
Fahrenheit[3] = 20 Celsius[1] = -5
Fahrenheit[3] = 20 Celsius[2] = -5
Fahrenheit[3] = 20 Celsius[3] = -4
Fahrenheit[3] = 20 Celsius[4] = -3
Fahrenheit[3] = 20 Celsius[5] = 10536
Fahrenheit[4] = 25 Celsius[0] = -6
Fahrenheit[4] = 25 Celsius[1] = -5
Fahrenheit[4] = 25 Celsius[2] = -5
Fahrenheit[4] = 25 Celsius[3] = -4
Fahrenheit[4] = 25 Celsius[4] = -3
Fahrenheit[4] = 25 Celsius[5] = 10536
Fahrenheit[5] = 17 Celsius[0] = -6
Fahrenheit[5] = 17 Celsius[1] = -5
Fahrenheit[5] = 17 Celsius[2] = -5
Fahrenheit[5] = 17 Celsius[3] = -4
Fahrenheit[5] = 17 Celsius[4] = -3
Fahrenheit[5] = 17 Celsius[5] = 10536
(int)0


OK, we avoid providing complete code here but I'm going to do it since you need some perspective. Besides, I don't believe this is homework and you're putting in a lot of effort. Kudos for that! You're starting to get the idea but you are WAY over-complicating things.

Look at how simple this app can be (and I added the ability to specify a starting and ending temperature, plus a step so you don't have to see every temperature between them):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	int start, end, step;

	cout << "Enter Start Temp: "; cin >> start;
	cout << "Enter End Temp: "; cin >> end;
	cout << "Enter Temp Step: "; cin >> step;

	for(int f=start; f<=end; f+=step)
	{
		cout << f << "F = " << (f-32)*5/9 << "C" << endl;
	}

	return 0;
}


Look Ma! No arrays!

I suggest you try some of the more simple coding problems and get comfy with the basics. That way when you get to the tough stuff you'll have a solid foundation and things won't be so confusing. There are plenty of good beginner problems around here, just do a search.

Almost forgot: Please use code tags when you post. Select the code part(s) in your posts and click the <> Format: button on the right. It makes it SO much easier to read other peoples code and that makes it much more likely that you will get somebody to help you.
Thank you, Cnoeval. I really appreciate your HELP!
Cnoeval, wow, it is a simple as only possible!
Topic archived. No new replies allowed.