Questions ab an answer...

This is a discussion on Questions ab an answer... within the C Programming forums, part of the General Programming Boards category; i don't have an answer key or anything for this textbook, but did i get this correct? #7- Rearrange the ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    Questions ab an answer...

    i don't have an answer key or anything for this textbook, but did i get this correct?

    #7- Rearrange the square3.c program so that the FOR loop initializes 'i', tests 'i', and increments 'i'. Don't rewrite the program; in particular, don't use any multiplications.

    Code:
    * SEC6.3 EX7      */
    /* DATE: 08-08-02  */
    
    #include <stdio.h>
    
    int main()
    {
    	int i, n, odd, square;
    
    	printf("This program prints a table of squares.\n");
    	printf("Enter number of entries in table: ");
    	scanf("%d", &n);
    	
    	square = 1;
    	odd = 3;
    	for (i = 1; i <= n; odd+= 2) {
    		printf("%10d%10d\n", i, square);
    		++i;
    		square += odd;
    	}
    
    	return 0;
    }
    ...and here's my code from the square3.c program:

    Code:
    /* Prints a table of squares using an old method */
    /* DATE: 07-24-02								 */
    
    #include <stdio.h>
    
    int main()
    {
    	int i, n, odd, square;
    
    	printf("This program prints a table of squares.\n");
    	printf("Enter number of entries in table: ");
    	scanf("%d", &n);
    
    	i = 1;
    	odd = 3;
    	for (square = 1; i <= n; odd+= 2) {
    		printf("%10d%10d\n", i, square);
    		++i;
    		square += odd;
    	}
    
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'd say you need to have another go

    Look at this part of the question:
    ... so that the FOR loop initializes 'i', tests 'i', and increments 'i'.
    This says to me that you need a for statement that looks like this
    Code:
    for (i = 1; i <= n; i++) {
    ... and the appropriate code moved around to make this work.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for (i = 1; i <= n; odd+= 2) {
    Simple enough, look into the usage of the comma operator and throw the increment for i into the loop.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    would this be correct? :
    Code:
    /* SEC6.3 EX7      */
    /* DATE: 08-09-02  */
    
    #include <stdio.h>
    
    int main()
    {
    	int i, n, odd, square;
    
    	printf("This program prints a table of squares.\n");
    	printf("Enter number of entries in table: ");
    	scanf("%d", &n);
    	
    	square = 1; 
    	odd = 3;
    	for (i = 1; i <= n; i++, odd+= 2) {
    		printf("%10d%10d\n", i, square);
    		square += odd;
    	}
    
    	return 0;
    }

  5. #5
    Unregistered
    Guest

    Question

    /* DATE: 08-09-02 */

    #include <stdio.h>

    int main()
    {
    int i, n, odd, square;

    printf("This program prints a table of squares.\n");
    printf("Enter number of entries in table: ");
    scanf("%d", &n);

    square = 1;
    odd = 3;
    for (i = 1; i <= n; i++, odd+= 2) {
    printf("%10d%10d\n", i, square);
    square += odd;
    }

    return 0;
    }

    +++++++++++++++++++++++++++++++++++++++
    What did you get (answer)when you typed the program in like this???

    Was is correct?? Let us know.

    Mr. C.

  6. #6
    Unregistered
    Guest
    I got the right output... but I got the correct output when using the code that I thought was previously correct... so I'm hoping one of you guys will know the answer to this C prog Q!

  7. #7
    JohnMayer
    Guest
    ...btw, forgot to sign in (the unregistered response before this one is mine's~!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    I got the right output... but I got the correct output when using the code that I thought was previously correct... so I'm hoping one of you guys will know the answer to this C prog Q!
    So, in summary we started with this:
    Code:
    for (i = 1; i <= n; odd+= 2)
    and the exercise was change it so....
    the FOR loop initializes 'i', tests 'i', and increments 'i'
    And you made it into:
    Code:
    for (i = 1; i <= n; i++, odd+= 2) {
    Do you think all the criteria has been met? It looks like it to me.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Unregistered
    Guest
    yea~!!!!!!!!!!!!!!! thanx so much Hammer (and everyone else) for your help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 08:42 AM
  3. 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, 09:26 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 03:00 AM

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