Hello Steve1029,
Looking at your code the first thing I notice is the use of {}s. Whatever style you choose,
https://en.wikipedia.org/wiki/Indentation_style , be consistant. Although the compiler does not care the person reading it does. Personally I like the "alman" style.
A few blank lines also help in readability.
Variable length Arrays, or (VLAs) are not allowed in C++, as far as I know. The compiler needs to know the size of the array at compile time to set aside the correct space on the stack for the array. What is in the []s needs to be a variable defined as a constant or an actual number. If you need to input the size of the array you could create a dynamic array on the heap with the keyword "new".
As I understand it C and some very old C++ compilers do allow VLAs.
The comments in the program should be a good start.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
int main()
{
int n;
// <--- Needs a prompt.
cin >> n;
int trans[n]; // <--- VLAs not allowed in C++.
int price[n];
int t[n];
for (int i = 1; i <= n; i++)
{
std::cout << "\n Enter trans: ";
cin >> trans[i];
// <--- Needs a prompt.
cin >> price[i];
// <--- Needs a prompt.
cin >> t[i];
}
int coupon_valid[n];
int coupon_t[n];
int actual_price[n];
for (int j = 1; j <= n; j++)
{
if (j == 1)
{
actual_price[j] == price[j];
// to demonstrate the validation time and price of the coupon when using bus
}
else if (trans[j] == 0)
{
// coupon_t[j]=t+45;
// coupon_valid[j]=price[j];
// }
for (int k = j - 1; k >= 1; k--)
{
// if(trans[k]==1 && coupon_t[k-1]>t[k] && coupon_valid[k-1]>price[k]){
if (trans[k] == 1 && coupon_t[k - 1] > t[k] && price[k] > price[j])
{
// actual_price==0;
coupon_valid[j] = price[k];
}
}
}
else
{
actual_price[j] = price[j];
}
}
}
int total = 0; // <--- Are these lines a function or should it be part of main?
for (int q = 1; q <= n; q++)
{
total = total + actual_price[q];
}
cout << total;
return 0;
}
|
Changing the arrays to vectors would be a good start. Also you could make use of "n", should be a better name not a single letter, to give the vector a size when defined.
Your for loops start at (1), but C++ is zero based, so you are missing element (0) zero of your arrays.
Be careful using "<=" in your for loops. Sometimes it can give you one extra loop that you do not want.
This is what I see without compiling the code. Compiled it gave me 31 errors and 4 warnings. most of which had to do with VLAs and the code at the end after "main".
Andy