Array

Pages: 12
hello i have done this work and id like to check my work plz if its correct



I have to modify this program so that it also asks the user to enter a number from 1-10. When the user enters the number 1, the program should update only the first price in the array. When the user enters 2, the program should update only the 2nd price, and so on. It says to use a loop that stops prompting the user when he or she enters a number that is either less than or equal to 0 or greater than 10,after run the program .increase the second price by 10%, increases the tenth price by 2 %, and decrease the first price by 10%."

here is the program

//Intermediate25.cpp - increases the prices stored in an
//array and then displays the increased prices
//Created/revised by <your name> on <current date>

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << fixed << setprecision(2);

//declare array
double prices[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};

system("pause");
return 0;
} //end of main function


this is what i modified

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << fixed << setprecision(2);

//declare array
double prices[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};
int iIndex;
double price = 0.0;
do {
cout << "Please enter the index of the price to update (1-10) > ";
cin >> iIndex;
if (iIndex < 1 || iIndex > 10) break;
cout << "Please enter new price> ";
cin >> price;
prices[iIndex-1] = price;
} while(true);
system("pause");
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Intermediate25.cpp - increases the prices stored in an
//array and then displays the increased prices
//Created/revised by <your name> on <current date>

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << fixed << setprecision(2);

//declare array
double prices[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};

system("pause");
return 0;
}	//end of main function


this is what i modified 

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
cout << fixed << setprecision(2);

//declare array
double prices[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};
int iIndex;
double price = 0.0;
do {
cout << "Please enter the index of the price to update (1-10) > ";
cin >> iIndex;
if (iIndex < 1 || iIndex > 10) break;
cout << "Please enter new price> ";
cin >> price;
prices[iIndex-1] = price;
} while(true);
system("pause");
return 0;
}


I didn't do anything I just put code tags makes it easier for people
Last edited on
lolll thanks that helps.... but what do you think about my codes
@clavier (30)

You have done 30 posts and should know by now to always use code tags - edit your original post, select your code then press the <> button on the right.

Do not use infinite loops, instead decide what the exit condition is going to be, and use that. I prefer not to use do loops, unless I really have to - which is rare. Seen as you have a numerical limit, a for loop makes more sense for this situation.

HTH
I'm not sure what you're trying to do after selecting an index within the array.

From what I'm reading, your program ends if you enter an incorrect index (outside the bounds of the index size). And you're entering a new price for that index, instead of changing it by a percent like you said.
confusedddddd.... so what do u think that i have to change
I'm not sure. It depends on what you need to do with the array.
well seems u did not read the question above but its ok
i just make some changes on my codes... what do you think guys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
include <iostream>
#include <iomanip>
using namespace std;

int main()
{

       cout << fixed << setprecision(2);

      //declare array
       double prices[10] = {10.5, 25.5, 9.75, 6.0, 35.0, 100.4, 10.65, .56, 14.75, 4.78};

      //declare variable
      int num = 0;
      double percent = 0.0;

		 cout<<"Enter a number from (1-10): ";
		 cin>>num;
        
        while (num > 0 && num < 11)  
        {
                cout <<" Enter the percent as a whole: ";
                cin>>percent;
 
                percent *= .01;
 
                prices[num-1] = prices[num-1] + (percent * prices[num-1]);


        //get next number 
		 cout<<"Enter the next number from (1-10): ";
		 cin>>num;
		}// end the loop 
       
      for ( num = 0; num < 10; num +=1)
       {
               cout<< prices[num] <<endl;
       }//end for
       return 0;
}//end of main function
[code]
[/code]
Last edited on
I think you should use code tags :) That way people can help you better.
i tried but i dnt know how to use them... can you help me plzzz
When you are typing in this box to answer me back right now look to your right there is a format: with boxes choose <> the first one one the left. and past your code within the two boxes when they appear.
i got it i just do it

thanks...

so what u think about my codes, i ran the program and its does compile but not sure if its correct
Well,
I'm not sure what you mean by correct. I know what you said above I read that but I'm still unsure. I ran it on mine. It compiled. It asks me to enter a percent as a whole. Then it asks me to enter the next number from 1-10. What is it supposed to be doing actually? Sorry, I'm just unclear about what you mean by correct. Correct is relative.
well thats the original question

asks the user to enter a number from 1-10. When the user enters the number 1, the program should update only the first price in the array. When the user enters 2, the program should update only the 2nd price, and so on. It says to use a loop that stops prompting the user when he or she enters a number that is either less than or equal to 0 or greater than 10,after run the program .increase the second price by 10%, increases the tenth price by 2 %, and decrease the first price by 10%."

Sometimes if I'm curious what is going on I will display the array before I do something then display it after to see if it has done the job I wanted. You should try that and see if the array is being changed like you want it to.
i did run the program and it does compile its just the question seems weird for me when they astek me to increase by 10%,next increase the tenth price by 2% finally decrease the first price by 10%
Did you do that?
i did for 10 percent but i dont know how to do it for the others
after run the program .increase the second price by 10%, increases the tenth price by 2 %, and decrease the first price by 10%

What does it mean "after run the program"
Pages: 12