Help! how to put vectors & loops in my assign

October 14, 2010 at 19:06:04
Specs: Windows Vista
I am totally lost on the topic of vectors and loops. My teacher wants me to incorporate it into my C++ assignment, but I don't know where to start. Here is what he is asking for:

In solving the problem, you must use the following commands and elements of C++:

Vectors
Loops
Conditionals
.
They want to know both the highest and lowest temperatures of the day. You'll need to program this new report format into the system. Here's the proposed format:

Enter reported temperatures...
Weather Station 0 = 45
Weather Station 1 = 44
Weather Station 2 = 43
Weather Station 3 = 51
Weather Station 4 = 50

========NGS Temperature Data Report========
Fahrenheit Celsius
-------------------------------------------
Lowest Temperature: 43.0 6.11
-------------------------------------------
Highest Temperature: 51.0 10.6
-------------------------------------------
Mean Temperature: 46.6 8.11
-------------------------------------------
Raw Data...
WeatherStation 0 = 45.0 7.22
WeatherStation 1 = 44.0 6.67
WeatherStation 2 = 43.0 6.11
WeatherStation 3 = 51.0 10.6
WeatherStation 4 = 50.0 10.0
-------------------------------------------
========End Temperature Data Report========

Where would I even begin? I'm sorry, but I am new to this so everything seems so difficult. I got though my first assignment easily but this one is kind of confusing. I've read my book a bunch of times but I still don't know how to input vectors and loops. Can someone please help me?


See More: Help! how to put vectors & loops in my assign

Report •


#1
October 14, 2010 at 19:14:37
Step 1: Declare a vector of, let's say, double.
Step 2: Declare at least 3 more doubles, and probably an int, too.
Step 3: Ask for user input.
Step 4: Calculate the min/max/mean.
Step 5: Output.

EDIT: If you need help with any particular step, let us know and show us your code. The STL syntax (and template syntax in general) can throw people for a loop.

How To Ask Questions The Smart Way


Report •

#2
October 17, 2010 at 09:51:39
#include <stdio.h>
#include <conio.h>

main()
{
float x[4],y,z;
int i;

printf("Enter reported temperatures...\n");

for(i=0;i<=4;i++)
{
printf("Weather station %d = ", i);
scanf("%f", &x[i]);
}

printf("\n=====NGS Temperature Data Report=====\n\n");
printf("Fahrenheit Celsius\n\n");

for(i=0;i<37;i++)
{
printf("-");
}
printf("\n\n");


if(x[0] < x[1] && x[0] < x[2] && x[0] < x[3] && x[0] < x[4])
{
y =(x[0]-32)*5/9;
printf("Lowest Temperature: %.1f %.2f\n", x[0],y);
}

else if(x[1] < x[0] && x[1] < x[2] && x[1] < x[3] && x[1] < x[4])
{
y =(x[1]-32)*5/9;
printf("Lowest Temperature: %.1f %.2f\n", x[1],y);
}

else if(x[2] < x[0] && x[2] < x[1] && x[2] < x[3] && x[2] < x[4])
{
y =(x[2]-32)*5/9;
printf("Lowest Temperature: %.1f %.2f\n", x[2],y);
}

else if(x[3] < x[0] && x[3] < x[1] && x[3] < x[2] && x[3] < x[4])
{
y =(x[3]-32)*5/9;
printf("Lowest Temperature: %.1f %.2f\n", x[3],y);
}

else if(x[4] < x[0] && x[4] < x[1] && x[4] < x[2] && x[4] < x[3])
{
y =(x[4]-32)*5/9;
printf("Lowest Temperature: %.1f %.2f\n", x[4],y);
}


printf("\n");

for(i=0;i<37;i++)
{
printf("-");
}
printf("\n\n");


if(x[0] > x[1] && x[0] > x[2] && x[0] > x[3] && x[0] > x[4])
{
y =(x[0]-32)*5/9;
printf("Highest Temperature: %.1f %.2f\n", x[0],y);
}

else if(x[1] > x[0] && x[1] > x[2] && x[1] > x[3] && x[1] > x[4])
{
y =(x[1]-32)*5/9;
printf("Highest Temperature: %.1f %.2f\n", x[1],y);
}

else if(x[2] > x[0] && x[2] > x[1] && x[2] > x[3] && x[2] > x[4])
{
y =(x[2]-32)*5/9;
printf("Highest Temperature: %.1f %.2f\n", x[2],y);
}

else if(x[3] > x[0] && x[3] > x[1] && x[3] > x[2] && x[3] > x[4])
{
y =(x[3]-32)*5/9;
printf("Highest Temperature: %.1f %.2f\n", x[3],y);
}

else if(x[4] > x[0] && x[4] > x[1] && x[4] > x[2] && x[4] > x[3])
{
y =(x[4]-32)*5/9;
printf("Highest Temperature: %.1f %.2f\n", x[4],y);
}

printf("\n");

for(i=0;i<37;i++)
{
printf("-");
}
printf("\n\n");


z=(x[0]+x[1]+x[2]+x[3]+x[4])/5;
y=(z-32)*5/9;
printf("Mean Temperature: %.1f %.2f\n", z,y);

printf("\n");

for(i=0;i<37;i++)
{
printf("-");
}
printf("\n\n");

printf("Raw Data..\n");

for(i=0;i<5;i++)
{
y=(x[i]-32)*5/9;
printf("WeatherStation %d = %.1f %.2f\n", i,x[i],y);
}

for(i=0;i<37;i++)
{
printf("-");
}
printf("\n\n");

printf("\n=====End Temperature Data Report=====\n\n");


getch();
}


Report •

#3
October 19, 2010 at 21:09:45
Hey! This is what I have so far...but i'm not sure if its right..i'm still confused.

/*
title: NGS Temperature Data Report.cpp
description: illustrates the highest and lowest temperatures of the day.
*/
#include <iostream>
#include <string>
using namespace std;

int main()
{
int Count, Sum = 0;
string Value;
double List[51];

cout << "Enter reported temperatures..." << endl;
cout << "Fahrenheit" << endl;
cout << "Weather Station 0 = 45" << endl;
cout << "Weather Station 1 = 44" << endl;
cout << "Weather Station 2 = 43" << endl;
cout << "Weather Station 3 = 51" << endl;
cout << "Weather Station 4 = 50" << endl;

cout << "========NGS Temperature Data Report========" << endl;
cout << "Raw Data..." << endl;
cout << "Enter Values Below, Stop To Quit" << endl;
for(Count = 0 ; Count < 51 ; Count++) {
cout << "Enter Value: ";
cin >> Value;
if(Value == "Stop")
break;

List[Count] = atoi(Value.c_str() );
}

int K;

for(K = 0 ; K < Count ; K++)
Sum += List[K];

for(K = 0 ; K < Count ; K++)
cout << "List[" << K << "] = " << List[K] << endl;
if(Count > 0)
cout << "Mean Temperature = " << Sum / Count << endl;
else
cout << "No Values Entered" << endl;

int Smallest = List[0];

for(K = 1 ; K < Count ; K++)
if(List[K] < Smallest)
Smallest = List[K];
cout << "Lowest Temperature = " << Smallest << endl;

int Largest = List[0];

for(K = 1 ; K < Count ; K++)
if(List[K] > Largest)
Largest = List[K];
cout << "Highest Temperature = " << Largest << endl;
cout << " Fahrenheit Celsius " << endl;
cout << "-------------------------------------------" << endl;
cout << "Lowest Temperature: 43.0 6.11" << endl;
cout << "-------------------------------------------" << endl;
cout << "Highest Temperature: 51.0 10.6" << endl;
cout << "-------------------------------------------" << endl;
cout << "Mean Temperature: 46.0 8.11" << endl;
cout << "-------------------------------------------" << endl;
cout << "Raw Data..." << endl;
cout << "WeatherStation 0 = 45.0 7.22" << endl;
cout << "WeatherStation 1 = 44.0 6.67" << endl;
cout << "WeatherStation 2 = 43.0 6.11" << endl;
cout << "WeatherStation 3 = 51.0 10.6" << endl;
cout << "WeatherStation 4 = 50.0 10.0" << endl;
cout << "-------------------------------------------" << endl;
cout << "========End Temperature Data Report========" << endl;
system("pause");


Report •

Related Solutions

#4
October 19, 2010 at 21:11:10
Hey, this is what I have. Is this correct?

/*
title: NGS Temperature Data Report.cpp
description: illustrates the highest and lowest temperatures of the day.
*/
#include <iostream>
#include <string>
using namespace std;

int main()
{
int Count, Sum = 0;
string Value;
double List[51];

cout << "Enter reported temperatures..." << endl;
cout << "Fahrenheit" << endl;
cout << "Weather Station 0 = 45" << endl;
cout << "Weather Station 1 = 44" << endl;
cout << "Weather Station 2 = 43" << endl;
cout << "Weather Station 3 = 51" << endl;
cout << "Weather Station 4 = 50" << endl;

cout << "========NGS Temperature Data Report========" << endl;
cout << "Raw Data..." << endl;
cout << "Enter Values Below, Stop To Quit" << endl;
for(Count = 0 ; Count < 51 ; Count++) {
cout << "Enter Value: ";
cin >> Value;
if(Value == "Stop")
break;

List[Count] = atoi(Value.c_str() );
}

int K;

for(K = 0 ; K < Count ; K++)
Sum += List[K];

for(K = 0 ; K < Count ; K++)
cout << "List[" << K << "] = " << List[K] << endl;
if(Count > 0)
cout << "Mean Temperature = " << Sum / Count << endl;
else
cout << "No Values Entered" << endl;

int Smallest = List[0];

for(K = 1 ; K < Count ; K++)
if(List[K] < Smallest)
Smallest = List[K];
cout << "Lowest Temperature = " << Smallest << endl;

int Largest = List[0];

for(K = 1 ; K < Count ; K++)
if(List[K] > Largest)
Largest = List[K];
cout << "Highest Temperature = " << Largest << endl;
cout << " Fahrenheit Celsius " << endl;
cout << "-------------------------------------------" << endl;
cout << "Lowest Temperature: 43.0 6.11" << endl;
cout << "-------------------------------------------" << endl;
cout << "Highest Temperature: 51.0 10.6" << endl;
cout << "-------------------------------------------" << endl;
cout << "Mean Temperature: 46.0 8.11" << endl;
cout << "-------------------------------------------" << endl;
cout << "Raw Data..." << endl;
cout << "WeatherStation 0 = 45.0 7.22" << endl;
cout << "WeatherStation 1 = 44.0 6.67" << endl;
cout << "WeatherStation 2 = 43.0 6.11" << endl;
cout << "WeatherStation 3 = 51.0 10.6" << endl;
cout << "WeatherStation 4 = 50.0 10.0" << endl;
cout << "-------------------------------------------" << endl;
cout << "========End Temperature Data Report========" << endl;
system("pause");


Report •

#5
October 20, 2010 at 15:02:13
if you're required to use the vector, it may be a good idea to use it instead of the array. If you're confused about its syntax, then typedef it to something more recognizable.

typedef vector<double> vec_double;
vec_double List;

If you're after a double, then use atof and not atoi.

Note: if that function fails, it returns 0; if 0 is valid data for your program, then you may run into data being tainted. You might try sscanf instead as it will return the number of successfully stored conversions and not a possible data value.

int ret = sscanf(value.c_str(), "%lf", &List.at(index));

#include <vector>
#include <cstring> // required for sscanf
...
vector<double> List(51, 0.0);
or maybe 
vec_double List(51, 0.0);
...
List.at(index) = some_value;  // most like List[0-50]

HTH

-------------------------------------------
I have no aspiration for preaching. ;-)


Report •


Ask Question