Help me, Im a total beginner. Idk what to add to this program and what to edit out? On the first day there were 'k' items sold, every next day there were 'm' more items sold than the previous day. How many items were sold after 'n' days?I guess I failed with declaring 'k' and/or 'vk'
#include<iostream>
usingnamespace std;
int main (){
int n,k,m,vk=0;//vk=the sum, the answer
cin>>n;//number of days
cin>>k;//number of items sold the 1st day
cin>>m;//how much more were sold every next day
k=vk;
for(int i=1;i<=n; i++){
k=k+m;
vk=vk+k;
}
cout<<vk<<endl;
return 0;
}
Line 12 is a problem. You've just gotten k from the user, now you're overwriting it with vk which you've set to zero.
You will read code a lot more than you write it. Do yourself a favor and use descriptive variable names. See if you can tell what this code is doing based on the variable names:
#include <iostream>
usingnamespace std;
int main()
{
//replace bad variable names with good ones:
// n : numDays
// k : firstDaySales
// m : perDaySaleIncrease
// vk : total
int total = 0;
int numDays;
int firstDaySales;
int perDaySaleIncrease;
cin >> numDays;
cin >> firstDaySales;
cin >> perDaySaleIncrease;
//introduce a new variable (currentDaySales)
//to keep track of how many sales are occurring
//over subsequent days
int currentDaySales = firstDaySales;
for (int i = 1; i <= numDays; i++)
{
total = total + currentDaySales;
currentDaySales = currentDaySales + perDaySaleIncrease;
}
cout << total << endl;
return 0;
}
The new variable in for loops is always my mistake. It's really hard to understand the purpose of it in the program or me. I always feel like something is missing in the code but I just can't figure out what exactly. Thank you, I'll try to learn this situation.