Ok,
-First create an array(maybe of type double/float/int)
-Ask(using cout) the user to enter five prices.
-Use a loop(maybe for loop) to cin the prices into the array.
-More references[1].
int iaPrices[5] = { 5.00, 10.00, 15.00, 20.00, 25.00};
int n,result=0;
int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += prices[n];
}
cin <<”Enter a price: “;
cout << result;
return 0;
Should be:
1 2 3 4 5 6 7 8 9 10 11 12 13
int iaPrices[5] = { 5.00, 10.00, 15.00, 20.00, 25.00}; //No need to initialize
int n;
int main ()
{
for ( n=0 ; n<4/*4 here is the highest element's index*/ ; n++ /*telling the compiler that n should increase by 1 each time the loop completely runs*/)
{
cout<<"Enter a price: ";
cin>>iaPrices[n];
}
return 0;
}
How it works:
-First an array of type int is declared: int iaPrices[5], with 5 elements.
-Secondly(in the loop), The programme prompts the user to enter a number.
Remember n=0 for the first time the loop runs.
So when the user enters a number and hits enter, what he enters is
stored in iaPrice[0], which is the first element of the
array(read the link I posted in my first post for more info)
-Next:
When the loop ends, n++ that means, n increases by one.
So in this case, it becomes 1.
It then asks for another price, and puts it in iaPrice[1], which is
the second element.
-And so on...until i eventually becomes 4, then the loop "breaks".
Is the correct idiom for doing something 5 times. The array has subscripts 0 to 4, but n<5 becomes false when n == 5, so it will never access iaPrices[5]
int iaPrices [5] = {5.00, 10.00, 15.00, 20.00, 25.00};
int n;
int main(int argc, char *argv[])
{
for ( n=0 ; n<5; n++)
{
cout << "Enter a price: "; THIS IS NOT WORKING I GET A ERROR
cin>>iaPrices[n];
}
return 0;
}
system("PAUSE");
return 0;
}
int sum (num1, num2, num3, num4, num5)
{
int total;
total = num1 + num2+ num3+ num4= num5;
cout << "The sum of all five numbers is: ";
system("PAUSE");
return total;
}
int average (num1, num2, num3, num4, num5)
{
int average;
average = num1 + num2+ num3+ num4= num5 * 5;
cout << "The average of all five numbers is: ";
system("PAUSE");
return average;
}
Is the correct idiom for doing something 5 times. The array has subscripts 0 to 4, but n<5 becomes false when n == 5, so it will never access iaPrices[5]
iaPrices has 5 elements with index's 0-4. So let's agree to disagree.
int iaPrices [5] = {5.00, 10.00, 15.00, 20.00, 25.00};
int n;
int main(int argc, char *argv[])
{
for ( n=0 ; n<5; n++)
{
cout << "Enter a price: "; THIS IS NOT WORKING I GET A ERROR
cin>>iaPrices[n];
}
return 0;
}
system("PAUSE");
return 0;
}
int sum (num1, num2, num3, num4, num5)
{
int total;
total = num1 + num2+ num3+ num4= num5;
cout << "The sum of all five numbers is: ";
system("PAUSE");
return total;
}
int average (num1, num2, num3, num4, num5)
{
int average;
average = num1 + num2+ num3+ num4= num5 * 5;
cout << "The average of all five numbers is: ";
system("PAUSE");
return average;
}
That's because, you must include <iostream> not <stdio.h>
You must also use the namespace std;
~ If you want to access every index of an array with size five you need: for (int i = 0 ; i < 5; i++)~
@rtom40: Line whatever is giving you an error because you are not including iostream. If you want to include stdio, then you need scanf/printf rather than cin/cout
As far as declaring arrays, this method is more preferable:
1 2 3 4 5
constunsignedint SIZE = 5;
int myArray[SIZE];
for (int i = 0 ; i < SIZE; i++)
myArray[i] = i;
The libraries are because I am using Dev C++ not sure if that is why its not working
the #include <stdio.h> is automatically added
#include <iostream> Will not let me use this??????????/
#include <stdlib.h>
using namespace std; Will not let me use this???????????????????
int iaPrices [5] = {5.00, 10.00, 15.00, 20.00, 25.00};
int n;
int main(int argc, char *argv[])
{
for ( n=0 ; n<5; n++)
{
cout << "Enter a price: ";
cin>>iaPrices[n];
}
return 0;
}
system("PAUSE");
return 0;
}
int sum (num1, num2, num3, num4, num5)
{
int total;
total = num1 + num2+ num3+ num4= num5;
cout << "The sum of all five numbers is: ";
system("PAUSE");
return total;
}
int average (num1, num2, num3, num4, num5)
{
int average;
average = num1 + num2+ num3+ num4= num5 * 5;
cout << "The average of all five numbers is: ";
system("PAUSE");
return average;
}