views:

198

answers:

3

Suppose that temperature measurements were made on 7 days of 2009 in each of 5 cities .write a program that will read the city name followed by the temperature measurements of that city.The city name and measurements should be stored in two array .The program should find out the average temperature of each city.Also you should find the average temperature of each day? thank you

A: 

It's hard to know how to start without knowing the language. But if you're struggling with how to store this data, here's an idea in C++:

class CityData
{
  string City;
  float Temperatures[]; // array of temperatures
}

void main()
{
  CityData MyCityData[5]; // array of CityData
  MyCityData[0] = new CityData();
  MyCityData[0].City = "Dallas";
  MyCityData[0].Temperatures = { 50, 60, 63, 43, 75, 25, 64 };
  // and so on
}

Hopefully you can use this to accomplish the rest.

Steve Wortham
It is a good idea. However, arrays and loops are typically tought way before classes.
Robert Venables
I didn't know C has classes
NickLarsen
Correction: C++. ;)
Steve Wortham
@Steve: See my comment to Nick above. You're only hurting yourself (and other developers) by providing actual code instead of concepts or at best pseudo-code. (Particularly for posters who can't even identify the language for which they need help.)
Ken White
@Ken - That's why I didn't solve the problem -- I only provided a starting point.
Steve Wortham
A: 

Sounds like you need an array of numbers (not hard) and an array of strings (easy) or an array of characters (not hard).

You then get two index variables. The first indexes for the city (c) and the second indexes for the temperature (t).

You then setup a loop for the city. Set the index (c) to your language's array starting point (in most languages this is 0).

Read the city name and store it in the name array indexed to 0.

Next you want to setup a loop for the temperature. Again, start (t) at the beginning index.

Now the fun begins. You need to read the temperature and store it in the array at the position [t + c* MaxTemps]. (MaxTemps should be 7 in your case).

Read in all the temps increasing the index as you go.

Then increase the city index and read in the next city name. If you are using an array of strings, just put the next city name in the second position. If instead you are using an array of characters, you will need to have an array that is [maxChars * maxCities] long. You will then need to put the second name in the [c * maxChars] spot.

Now you should reset the temperature index and read in the temperatures for the next city.

Continue looping until all data has been read.

Output is similar to input. To get the average of the city read the data from [t + c * MaxTemps] ... [MaxTemps -1 + c*MaxTemps].

To get the average of each day, read the data from [t + c*maxTemps] ...[t + (maxCities -1) * MaxTemps]

Good luck.

Jeffery Williams
A: 

import java.util.*; class CityData { public static void main(Strang args[]) int[][]b=new int[8][6] string[][]a=new string[2][2]

Scanner cp=new scanner(System.in)

System.out.println("Enter your first data"); for(i=0;i<=1;i++) a[i][j]=cp.next[];

System.out.println("Enter your seconed data"); for(i=0;i<=1;i++){ for(j=0;j<=1;j++)}

a[i][j]=cp.nextint(); for(i=0;i<=1;i++){ for(j=0;j<=1;j++)} }} This is my answer but it without the avaraage because I dont know how can I write it

have alook