# Thread: some time to spare?

1. ## some time to spare?

I am preparing for programing exams at uni, Could some one please help me with these problems?

1. Indicate why the following pseudo-code is likely to produce incorrect output.
Indicate what needs to be done to make it correct.
Code:
```algorithm summation
integer count, sum
begin
for count  <--1 to 10 do
sum <-- sum + count
endfor
output 'Sum of numbers between 1 & 10 is ',sum
end algorithm```

2. What is the value of variable i and of variable j on line 6 below? Explain your answer.
Code:
```1    i=0;j=0;
2    while (i<10)
3    {
4         j=j+2;
5    }
6    // What is the value of variable i and variable j?```

3. Explain why the fscanf function has an '&' in front of the variable names for integers, floats, etc. For example, fscanf(input,"&#37;d",&age),and explain why the fscanf function does not have an '&' in front of the variable names for arrays. For example, fscanf(input,"%s",string_array).

4. Give a pseudo-code example to illustrate how a professional programmer would get a positive integer value from a user. Explain your design decisions.

5.Give the pseudo-code const and var declarations that could be used to hold information about a student. The information required is their name, address, student number, email address and telephone number.

6. Give separate fprintf statements that will display :

i) an integer variable height with a field width of 10
characters
ii) a character string stored in variable name
iii) a double floating point number with a field width of 8, and
3 decimal places that is in variable weight.
iv) a literal string "hello world.\nHow are you?"
v) an integer variable fred using a left-justified format
in a field width of 8 places.

2. Homework policy

How about you post what you THINK are the right answers, and then we can comment on ways to improve it?

--
Mats

3. ok this is what i think: (most likely wrong)

1. The program hasnt asked the user for any values and it hasnt stored the value. also i think it needs to initialize a value for count

2. i=0 and j=2

4. if value<0 then
output ' please enter a positive value'
else
do....etc

5. int telephone
char student-number [7] // since our student numbers have max 7 numbers
char name []
char email []

6.

i) fprintf(output,"%10c", height);

ii) fprintf(output,"%s", name);

iii) fprintf(output,"%8.3lf", weight);

iv)no idea

v) fprintf(output,"%8d", fred);

4. ok this is what i think: (most likely wrong)

1. The program hasnt asked the user for any values and it hasnt stored the value. also i think it needs to initialize a value for count
=====
No initialization of the var count. No user input needed.

2. i=0 and j=2
=====
i = 0, j = ?? because it's an endless loop. i will always be less than 10 since it doesn't change it's value. j will go to the compiler's highest (or within 1 of that highest) value for that datatype, and then "loop back" to it's lowest value (or lowest value + 1), and start climbing up by two's, from there.

You're not giving me a trick question before my morning java are you?

=====
scanf() and fscanf() need a pointer (address). Strings are their own base address. Variables whose names are not their own base address, need the address of operator '&' included.

Arrays of any type include their own base address:

&ArrayName[0] is the address at the base (first element) address of the array ArrayName.
ArrayName, by itself, is also the base (first element) address of the array ArrayName.

5. Thanks a lot adak you are a champ, however, what about questions 4,5,6?

6. Originally Posted by juststartedC
Thanks a lot adak you are a champ, however, what about questions 4,5,6?
char student-number [7] // since our student numbers have max 7 numbers

This could be an int student-number
In fact you'll never will be able to enter 7 digits in char student-number[7]. You forgot that this is a string and it will have a '\0' at the end of it.

>iv) a literal string "hello world.\nHow are you?"
fprintf( output, "&#37;s", "hello world.\nHow are you?" );

>v) an integer variable fred using a left-justified format in a field width of 8 places.
You wrote: fprintf(output,"%8d", fred); That's right justified. "%-8d" will make it left justified.

>i) an integer variable height with a field width of 10 characters
Just another way: fprintf( output, "%*d", 10, height );

7. Thanks alot aia.

Regarding
iv) a literal string "hello world.\nHow are you?"
fprintf( output, "%s", "hello world.\nHow are you?" );
wouldnt the \n take it to the nex line thus apearing like this:?

Hello World
How are you?

8. Originally Posted by juststartedC
Thanks alot aia.

Regarding

wouldnt the \n take it to the nex line thus apearing like this:?

Hello World
How are you?
You could always tested by writing a little piece and compiling it, using printf

9. so what fprintf statement should i write to display it like this

Hello world.\nHow are you?

say if i write it like this :

fprintf(output,"Hello world.\n\nHow are you?");

Would it display one and go to the next line for the other \n ???

10. close: \\

11. In other disciplines you might learn by asking just questions. No in programming.
Why don't you compile it and see what it does?
I am not been rude I am giving you a valuable suggestion.
In fact why don't you try to display \n in the screen using printf()

12. unfortunately i dont have a compiler, and the person who actually owns this laptop doesnt want me to install one, so i cant try the commands otherwise i would have found out the solution by now, that why im asking some one to help me.

13. Does the laptop have a USB port? put it on the usb memory and run it from there.

14. anyone out there knows what fprintf statement would display exactly the following:

hello world.\nHow are you?