couple questions...

This is a discussion on couple questions... within the C Programming forums, part of the General Programming Boards category; how do i write a value of a float to standard output? I thought it'd be the same as putting ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    38

    couple questions...

    how do i write a value of a float to standard output?

    I thought it'd be the same as putting an int which would be

    Code:
    printf ( "%d\n", fraction );
    but apparently not =/

    and is it written the same way for double?

    also this question.....

    Write a complete program that
    declares an integer variable,
    reads a value from the keyboard into that variable, and
    writes to standard output the square of the variable's value.

    this is waht i wrote

    Code:
    #include <stdio.h>
    int main ()
    {
    int x ; {
    scanf ( "%d\n", &x);
    x = x* x;
    printf ( "%d\n", x);
    }
    getchar ();
    }
    that part, the TA added it for me but the thing is, he doesnt speak english, so he couldnt explain what that is...

    is that how you get square of something? (looks like x = x times x which would square it)

    and there is this question...

    writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces.

    the square is x = x* x but how would i times it by two?

    i wrote

    Code:
    x = x*x;
    x = x* 2;
    but apparently its wrong.... and what does it mean separated by space ? put everything in 1 line?

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    how do i write a value of a float to standard output?
    With the %f format specifier:
    Code:
    printf("%f\n", fraction);
    and is it written the same way for double?
    Yes, but only with printf. You have to be careful to use %f for float and %lf for double when using scanf. There's a big difference because printf can promote a float to double while scanf cannot promote a pointer to float to a pointer to double.
    is that how you get square of something? (looks like x = x times x which would square it)
    Yea, that's certainly the easiest way.
    the square is x = x* x but how would i times it by two?
    x = x * 2, or the shorthand x *= 2.
    and what does it mean separated by space ?
    Whitespace doesn't mean anything in C, except where it does, so you can usually add whitespace wherever you want to make code easier to read. Your TA didn't do a good job of that, though.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Question #1 - Check out http://www.die.net/doc/linux/man/man3/printf.3.html. Look under "The conversion specifier". float and double are the same thing to printf().

    Question #2 - You're going to have to do the calculations within the printf() call instead of in a seperate statement since you need to print the values seperated by a space (yes, on the same line).
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    i'm curious why this isnt working then...

    question
    Write a complete program that
    declares an integer variable,
    reads a value from the keyboard into that variable, and
    writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces.

    Code:
    #include <stdio.h>
    int main ()
    {
    int x ; {
    scanf ( "%d\n", &x);
    x = x * 2;  x= x * x;
    printf ( "%d\n", x);
    }
    getchar ();
    }
    Last edited by seal; 08-29-2005 at 06:35 PM.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Code:
    x = x * 2  x= x * x;
    You need to separate these two operations with a semicolon or a comma:
    Code:
    x = x * 2;  x= x * x;
    But if you need to output both the the value multiplied by 2 and the value squared, you need to do the operation and print it separately, otherwise changes made in the first operation will affect the second:
    Code:
    #include <stdio.h>
    
    int main() {
      int x;
    
      scanf("%d", &x);
      printf("%d %d\n", x * 2, x * x);
    
      return 0;
    }
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    Quote Originally Posted by Narf
    Code:
    x = x * 2  x= x * x;
    You need to separate these two operations with a semicolon or a comma:
    Code:
    x = x * 2;  x= x * x;
    But if you need to output both the the value multiplied by 2 and the value squared, you need to do the operation and print it separately, otherwise changes made in the first operation will affect the second:
    Code:
    #include <stdio.h>
    
    int main() {
      int x;
    
      scanf("%d", &x);
      printf("%d %d\n", x * 2, x * x);
    
      return 0;
    }
    yeah i made a typo with that, i added it in though heh...

    that code didnt work either. Keeps saying
    Suggestion: You didn't write out the number read in.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    that code didnt work either.
    You're confusing "doesn't work" with "doesn't do exactly what you want". It works fine given correct input, and you should have no trouble modifying it to print x first.
    Just because I don't care doesn't mean I don't understand.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    Quote Originally Posted by Narf
    You're confusing "doesn't work" with "doesn't do exactly what you want". It works fine given correct input, and you should have no trouble modifying it to print x first.
    you're right sorry...

    Code:
    #include <stdio.h>
    
    int main() {
      int x;
    
      scanf("%d", &x);
      printf("%d %d", x * 2, x * x);
    
      return 0;
    }
    its doing what it exactly wants it to do now but its still saying its not correct.....

    i give up, imma have to ask the teacher tommorow, at least she speaks english lol
    Last edited by seal; 08-29-2005 at 07:05 PM.

  9. #9
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    declares an integer variable,
    Code:
    int x;
    reads a value from the keyboard into that variable, and
    Code:
    scanf("%d", &x);
    writes to standard output the variable's value, twice the value, and the square of the value, separated by spaces.
    Code:
    printf("%d %d %d\n", x, x * 2, x * x);
    Q.E.D
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 02:50 PM
  2. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. A couple of questions that someone might know the answer to...
    By Finchie_88 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-15-2005, 08:26 AM
  5. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM

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