0

What's the problem? (29.2 item of C++ study "Data Types, Arrays, Pointers")

#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; int p; int a; double sale; for (a=0; a<11; a++){ sale=a-a*p/100; cout<<items[0]<<items[1]<<items[2]<<items[3]<<items[4]<<items[5]<<items[6]<<items[7]<<items[8]<<items[9]; } return 0; }

29th May 2021, 5:03 PM
Vladimir Kushner
Vladimir Kushner - avatar
25 Respuestas
+ 3
Vladimir Why are you printing Hard Code values? Do you know outputs can vary on different inputs?
29th May 2021, 5:27 PM
A͢J
A͢J - avatar
+ 2
I don't know what you want to calculate through this program here you defined array which data type is double type after that u decleared two variables p a and another one sale What is the purpose of your sale expression inside loop becz u not using this calculation anywhere in program after that u printing indexes. You don't need to do work hard just apply loop and write simple statement cout Like this for (int i=0........; i++ ;) cout<<items[i];
29th May 2021, 5:47 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Vladimir That's not good for you. If you want to take shortcut then why you are trying to solve problem with Hard Code values. You will not learn coding like that.
29th May 2021, 6:03 PM
A͢J
A͢J - avatar
+ 2
Vladimir apart from what the others have said, I'm guessing the problem is with your output format. cout << 1 << 2; Will give the output 12 Are you sure the output is not separated by space or something? I can't say much or for sure because the problem is Pro-only and me, having no pro-subscription, cannot see the problem. Can you maybe copy the imstructions here?
29th May 2021, 6:06 PM
XXX
XXX - avatar
+ 2
Vladimir Like I said, that is a Pro-only problem and I cannot view it. Can you copy-paste the instructions here?
29th May 2021, 7:58 PM
XXX
XXX - avatar
+ 2
Vladimir bro you haven't taken user input p(discount %), Also what is logic in calc of sale, items element should be used for calc of sale And then what are u displaying....? Items elements but why you have asked to display final discounted prices .... I don't know what is your purpose of doing so but as far as I've understood your problem I've modified it here.........in attached https://code.sololearn.com/cfG5bfR1b04b/?ref=app
31st May 2021, 7:06 AM
saurabh
saurabh - avatar
+ 1
No, I just want complete the task to study.
29th May 2021, 5:39 PM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
I take an experience this way) From easy to hard
29th May 2021, 6:14 PM
Vladimir Kushner
Vladimir Kushner - avatar
29th May 2021, 6:19 PM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
The store sells products at different prices, which are represented in an array of double types (see the template). Write a program that takes the "discount percentage" as input data, and the discounted prices as output data in a single line in a given sequence, separated by a space (" "). Example of input data 25 Sample output data 375 9.3 70.5 33.75 2.25 60.75 750.675 63.75 67.5 0.75 26.25 To get the discounted price, use the formula: a – a*p/100, where "a "is the original price and" p " is the discounted price.
30th May 2021, 5:56 PM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
Vladimir sorry for the delay in answering. As for your latest question, `int p{};` is the same as `int p = 0;` It was a new syntax introduced in C++11. You don't need to worry about that now, you can learn about those things later. There are a lot of problems with your current code. In fact, your current code is nowhere near solving the problem. 1. First of all, `a` and `p1 should be of type double. This is because later in the code, you're doing `p/100` which, if `p` is int and p<100, will always return 0 as integer division would take place 2. The line `sale=....` is totally senseless. Firstly, `p` is uninitialized and thus has garbage value and second, `a` has to be the original price, whereas here it is just a loop counter. 3. Currently, the array is being printed in each iteration of the loop, *without performaing any operations*. The array *with discounted prices* has to be printed ONCE (Steps to get the solution in my next answer. Please wait for it)
31st May 2021, 5:40 PM
XXX
XXX - avatar
+ 1
The second value and so on isn't calculate correctly)
31st May 2021, 6:58 PM
Vladimir Kushner
Vladimir Kushner - avatar
+ 1
Vladimir sorry again. I didn't spot this mistake in my previous answer. The type of `a` should be double. This is because you're assigning `items[i]` to `a`, and `items` is an array of type double
31st May 2021, 7:12 PM
XXX
XXX - avatar
0
What does the 8-th string of code mean: p{} ? why can't I just use it: cin>> ?
31st May 2021, 5:29 PM
Vladimir Kushner
Vladimir Kushner - avatar
0
The solution should go like this: 1. Take the discount percentage as input, into a variable `p` of type double. 2. Make a for loop, which loops from 0-11 (0 till <12, as there are 11 nums in the array), with a variable `i` of type int 3. Inside the for-loop, calculate the sale with the formula given in the question (a - a * p/100) with a=items[i] (the ith price in the `items` array) and p=p. 4. Output the sale using cout, followed by a space. The statement would be `cout << calculated_sale << " ";` This should work as expected.
31st May 2021, 5:46 PM
XXX
XXX - avatar
0
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; double p; cin>>p; double sale; for (int i=0; i<12; i++){ int a; sale=a-(a*p/100.0); a=items[i]; cout<<sale<<' '; }
31st May 2021, 6:22 PM
Vladimir Kushner
Vladimir Kushner - avatar
0
Printing the zero first
31st May 2021, 6:26 PM
Vladimir Kushner
Vladimir Kushner - avatar
0
Vladimir Very close. Everything is correct except for 1 thing. In the line `sale = a - ....` You are using `a` without assigning any value to it. Instead, you're assigning a value to `a` *after* calculating the sale, which is wrong. To fix it, you need to move the line `a = items[i]` to before the line `sale = ....` So the first 3 lines of the for-loop would be, ``` int a; a = items[i]; sale = a - (a*p/100.0); ```
31st May 2021, 6:36 PM
XXX
XXX - avatar
0
Vladimir I am sorry, in my previous to previous answer, I said "loop from 0-11 (0 till <12,....". I actually had to say "0 till <11". So the for-loop would be for (int i =0; i < 11; i++) Also, you are missing a closing curly brace (}) at the end
31st May 2021, 6:41 PM
XXX
XXX - avatar
0
#include <iostream> using namespace std; int main() { double items[] = {500, 12.4, 94, 45, 3, 81, 1000.9, 85, 90, 1, 35}; double p; cin>>p; double sale; for (int i=0; i<11; i++){ int a; a=items[i]; sale=a-(a*p/100.0); cout<<sale<<' '; } return 0; }
31st May 2021, 6:56 PM
Vladimir Kushner
Vladimir Kushner - avatar