What is the purpose of this program?

  • Thread starter Thread starter shermaine80
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around two C programming snippets and their outputs. The first snippet initializes an array of integers and calculates its size, but contains an error in the loop condition, leading to undefined behavior when accessing the array. The second snippet calculates the value of `s` based on the expression `r % 10 + '0'`. The confusion arises from the addition of the character '0', which has an ASCII value of 48. Therefore, when `r` is 6, the expression evaluates to 54, not 6, as `6 % 10` results in 6, and adding 48 (the ASCII value of '0') gives 54. The discussion clarifies the use of the modulus operator and the ASCII representation of characters in C programming.
shermaine80
Messages
30
Reaction score
0
May i know what does this program does?

int days[20];
int size;

size = sizeof(days) / sizeof(int);
for (j =1; j <= szie; j++)
days[j] = 30;

Can anyone advise me? Thanks.
 
Technology news on Phys.org
May i know the output for this?

Hi,

May i know what's the output for this?

#include<stdio.h>
main()
{
int r; int s;
r = 6;
s = r%10 + '0';
printf("%d",s);
}

The output i get from c program is 6.
How come it's 6? I'm a bit puzzled. r%10 = ?
Can someone help me? Thanks.
 
May i know what does this program does?

int days[20];
int size;

size = sizeof(days) / sizeof(int);
for (j =1; j <= szie; j++)
days[j] = 30;

Can anyone advise me? Thanks.
It looks like

(1) It allocates an array (days) of 20 ints.
(2) It allocates an int. (size)
(3) It assigns the value 20 to size.
(4) It assigns the value 30 to the last 19 entries of days.
(5) It commits undefined behavior.


May i know what's the output for this?

#include<stdio.h>
main()
{
int r; int s;
r = 6;
s = r%10 + '0';
printf("%d",s);
}

The output i get from c program is 6.
How come it's 6? I'm a bit puzzled. r%10 = ?
Can someone help me? Thanks.
The output is 54, not 6.

% is the modular reduction operator. a%b is the remainder when you divide a by b. (I forget precisely how the signs work when a or b are negative)
 
Thanks! But could you advise me how come it's 54?
r%10 = 6%10 = 0.6, rite?
what does '0' means?
 
6%10 = 0.6, rite?
Not quite: remember that

numerator / demonator = quotient + (remainder / denominator)

'0' is a character: something of type char. If you look it up in the ASCII tables, you'll find that the character '0' is represented by the integral value of 48.

Now, if you had used %c instead of %d as your format specifier, printf would have printed the character that 54 represents: '6'.
 
thanks a lot! i get it :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top