some time to spare?

This is a discussion on some time to spare? within the C Programming forums, part of the General Programming Boards category; I am preparing for programing exams at uni, Could some one please help me with these problems? 1. Indicate why ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    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.
    Last edited by juststartedC; 11-16-2007 at 03:56 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Homework policy

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

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    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

    3. i honestly have no idea about this one.

    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 address []
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?

    3. i honestly have no idea about this one.
    =====
    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.
    Last edited by Adak; 11-16-2007 at 06:40 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    Thanks a lot adak you are a champ, however, what about questions 4,5,6?

  6. #6
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by juststartedC View Post
    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 );
    Last edited by Aia; 11-16-2007 at 09:43 PM.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    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. #8
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by juststartedC View Post
    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
    The answer is Yes.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    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. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    close: \\

  11. #11
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    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. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    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. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Does the laptop have a USB port? put it on the usb memory and run it from there.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    anyone out there knows what fprintf statement would display exactly the following:

    hello world.\nHow are you?

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21