Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to find the outputs of this programme but I can't. I don't understand it, please help me.

What I have tried:

C++
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
	char v[] = {'A', 'B', 'C', 'D', 'Z'};
	int m=0, k=0, x;
	while (v[k] != 'Z')
	{
		m += v[k] - 'A';
		k++;
	}
	printf("%d\n", m);
	for (m=5, k=7, x=0; --m, k>m; k-=2) x++;
	printf("%d\n", x);

system("pause");
return 0;
}
Posted
Updated 12-Apr-18 4:41am
v2

Compile and run it.

To know what happens at each step insert print statements showing the content of all variables inside the loops.

Or do it manually by writing it down on a piece of paper or in a text file:
m = 0, k = 0
v[k] = v[0] = 'A' = 64
v[k] - 'A' = 'A' - 'A' = 0
m += v[k] - 'A' -> m = 0
k++ -> k = 1
v[k] = v[1] = 'B' = 65
v[k] - 'A' = 'B' - 'A' = 65 - 64 = 1
m += v[k] - 'A' -> m = 1
...
Similar for the second loop:
m = 5, k = 7, x = 0
--m -> m = 4
k > m / 7 > 4 ? Yes, move on
x++ -> x = 1
k -= 2 -> k = 5

--m -> m = 3
k > m / 5 > 3? Yes, move on
x++ -> x = 2
k -= 2 -> k = 3

...
All you have to know is the execution order of for loops and what the comma operator does (see for example for loop - cppreference.com[^] and Other operators - cppreference.com[^] ).
 
Share this answer
 
Comments
CPallini 12-Apr-18 16:27pm    
5.
Quote:
I tried to find the outputs of this programme but I can't. I don't understand it, please help me.

There is no need to understand this program to find its output. The easy way is simply to run it, and you will get the outputs.
Quote:
Can you please describe me how to find the outputs of this programme?

This program is specifically crafted to be difficult to understand if just reading the code, but you can get help from your PC, the tool to use is the debugger.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
you better write cleaner code (more understandable) and extend the output for a better understanding like that:
C++
m=5, x=0;//not for the loop
printf("m = %d\n", m);
for ( k=7; k>m; k-=2) //with brace for loop body
{
  x++;
  --m;// in loop operation
  printf("x = %d\n", x);
}
printf("final x = %d\n", x);
The output is at the console (the black window).
 
Share this answer
 
Comments
Jochen Arndt 13-Apr-18 2:43am    
It is probably an assignment with given code.

Note also that your code will have a different result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900