However, I would like to know if this ans is also correct? |
Did you test it to find out?
First, the code doesn't compile, there's a missing
cout
at the start of lines 2 to 5.
Let's fix that, then try it:
1 2 3 4 5
|
cout << "Month \tIncome \tExpense" << endl;
cout << argv[1] << "\t" << argv[2] << "\t" << argv[3] << endl;
cout << argv[4] << "\t" << argv[5] << "\t" << argv[6] << endl;
cout << argv[7] << "\t" << argv[8] << "\t" << argv[9] << endl;
cout << argv[10] << "\t" << argv[11] << "\t" << argv[12] << endl;
|
The answer is, it does something similar, so is giving a reasonable output. However it differs in three ways.
First, your version prints out the income and expense values as character strings, while the 'model answer' prints them as integers. That could be very important if you wanted to do such things as calculate the totals or averages and so on.
Secondly, the original question asked this,
Read in the values from the command line, store them in three different arrays
month[], income[], and expense[] respectively, and display the data. |
Note Your code does not even attempt that part of the problem, hence it would be considered incorrect, simply for not meeting the specifications. At present it may seem unimportant, but often one part of a program builds on what has gone before, so missing out the foundations would potentially present problems at a later stage.
Thirdly, the 'model answer' uses a loop. That makes it more flexible. For example, imagine the program had to handle 100 sets of data instead of just 4. The model answer (which could be improved) would require the value '4' to be changed to '100', a minor change, while your alternative would involve adding many lines of code and carefully typing the suitable numbers into each index - a laborious and error-prone task.
and I also would like to know what is happening in the model ans for the following lines:1 2 3
|
month[i] = argv[i*3+1];
income[i] = atoi(argv[i*3+2]);
expense[i] = atoi(argv[i*3+3]);
|
|
Here, the way to understand the code is to break it down into smaller pieces.
Try this - see what is the output:
1 2 3 4 5 6 7
|
for (int i=0; i<4; i++)
{
cout << " i = " << i << " i*3 + 1 = " << i*3 + 1 << endl;
cout << " i = " << i << " i*3 + 2 = " << i*3 + 2 << endl;
cout << " i = " << i << " i*3 + 3 = " << i*3 + 3 << endl;
cout << "-----------------------\n";
}
|
Now you understand that part a little better,
This line should make more sense:
It stores the first command-line parameter which is the month name in an array. Here, the array simply uses a
char*
pointer to point to the string itself.
The other two lines introduce one more element, the
atoi()
function. Look it up, it is described as
Convert string to integer
http://www.cplusplus.com/reference/cstdlib/atoi/
That function is used to convert a character string such as "123" to an integer 123 for example. Strings are effectively just an array of characters, they need not be numbers at all. Integers are numbers, which can be used in calculations and so on
Hope that helps.