Help with Arrays

Pages: 12
Can you guys help me out with a program im trying to write...

program that prompts the user for 5 prices. You are to store the values in an array.
Im writing this in DEV C++
Last edited on
Post your code with code tags the <> button on the right, and we can help you from there.

Hope all goes well.

Edit: And any Compiler output.
Last edited on
Homework right?...

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].

---------REFERENCES------------
[1] http://cplusplus.com/doc/tutorial/arrays/

HTH,
Aceix.
Here is what I have remeber I am very new to this

int main()
{
int num1 ("10");
int num2 ("20");
int num3 ("30");
int num4 ("40");
int num5 ("50");

int num(0);

cout << endl
<< "Enter a number: ";
cin >> num1;

cout << endl
<< "Enter a number: ";
cin >> num2;

cout << endl
<< "Enter a number: ";
cin >> num3;

cout << endl
<< "Enter a number: ";
cin >> num4;

cout << endl
<< "Enter a number: ";
cin >> num5;

return 0;
}
int sum (num1, num2, num3, num4, num5)
{
That's not an array. Those are different variables. Did you read the link I provided?

This is how to declare an array--of type int: int iaPrices[5];

HTH,
Aceix.
like this

int prices [5] = { 5.00, 10.00, 15.00, 20.00, 25.00}

cout << endl
<< "Enter a number: ";
cin >> num1;

cout << endl
<< "Enter a number: ";
cin >> num2;

cout << endl
<< "Enter a number: ";
cin >> num3;

cout << endl
<< "Enter a number: ";
cin >> num4;

cout << endl
<< "Enter a number: ";
cin >> num5;

return 0;
}
int sum (num1, num2, num3, num4, num5)
{
Yes!...

... but using a for loop to fill the array is more convenient.
eg:

1
2
3
4
for(i=0;i<4;i++)
{
     cin>>prices[i];
}


If you analyze this, you'll notice how it works.

HTH,
Aceix.
Last edited on
How is this and if OK do I just add this 4 mores times for the 5 prompts?
apologize just not getting this concept

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;
TheIdeasMan wrote:
Post your code with code tags the <> button on the right,


You should know this by now, having done 13 posts.

I wish this site would have "Use Code tags" in large, bright red, flashing letters.

Sorry for being a grumpy drawers, but I seem to say this to almost every newbie.

Good Luck, am happy to help, Hope all goes well :+D
Your code:

1
2
3
4
5
6
7
8
9
10
11
12
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".

That is how it works.

HTH(Hope This Helps),
Aceix.
Last edited on
@Aceix

I disagree with the for loop:

for ( n=0 ; n<5 ; n++ )

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]

Here is what I have now

#include <stdio.h>
#include <stdlib.h>

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;
}
TheIdeasMan wrote:
I disagree with the for loop:

for ( n=0 ; n<5 ; n++ )

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.

Aceix.
OP wrote:
#include <stdio.h>
#include <stdlib.h>

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;

HTH,
Aceix.
~ 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
const unsigned int SIZE = 5;
int myArray[SIZE];

for (int i = 0 ; i < SIZE; i++)
  myArray[i] = i;
@Aceix

I ran this code, which is very similar to yours

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


int main()
{
	double iaPrices[5]= { 5.00, 10.00, 15.00, 20.00, 25.00};
	
	int n;
	for ( n=0 ; n<4 ; n++ ){
		
		cout<<"Price: " << n << " " << iaPrices[n] << endl;
		
	}
	return 0;
}


And had this output:


Price: 0 5
Price: 1 10
Price: 2 15
Price: 3 20


Still disagree ?
Last edited on
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;
}
TheIdeasMan wrote:
Still disagree ?

Ok. My mistake.

Thanks,
Aceix.
Last edited on
OP wrote:
#include <iostream> Will not let me use this??????????/
#include <stdlib.h>

Can't you remove the <stdio.h>? Cause I first used Dev-C++ but I did not use that header.

Aceix.
Is there a vague hope that we can get the OP to use code tags

It's really starting to rip my nightie (:=o)

code tags code tags code tags code tags code tags code tags code tags code tags code tags code tags
Pages: 12