Thread: Having some trouble with this C code

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    10

    Having some trouble with this C code

    1.
    Find the output:
    Code:
    
    
    Code:
    int x =5, y;
                        while (x >= 2) {
                                 y=1;
                    while (y <= x) {
                    printf (  *  );
                    y++; }
                    x--;
                    printf ( \n );   }
    2.
    
    Find the output of the following program segment.
    
            int   A [ ] = { 1, 3, 5, 2, 4, 7 };   int  i, h, j = 5;
            for ( i = 0;   i < 3;  i ++ ) {
            h = A [ i ];
            A [ i ] = A [ j ];
            A [ j ] = h;      j = j - 1; }
    
            for ( j = 0;    j < = 5;   j++ )    
                   printf (  %4d  , A [ j ] );
    3.
    
    Given the following main program, write a function which prints the average of array elements.
                    void  xyz  ( int [ ], int ); 
                        int   main ( ) {
                        int A [10 ] = {1,2,  . . . ., 10}; int n; \\ n refers to 10 elements.
                        xyz ( A, n ); 
                                           return 0;  }
    
    4.
    
    Given the following function, write the main program which calls the function and prints the results.
                    int    xyz ( int x, int y, int y ) {
                        int a, b;
                        a = x + y + z:
                        b = a/3;
                        return b;   }


    Here is what I believe is the answers,
    1.
    *
    *
    *
    *
    2.

    everything is printed backwards 7,4,2,5,3,1
    3.

    I'm stuck on this one

    4.

    stuck on this on as well

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Why can't you "find the output"?

    All you have to do is create an executable program, run it and there is your answer.

    Fair enough, these are only program snippets, but it's only a few lines of boilerplate code which is usually necessary to make it all work.

    Show us your actual code attempt at making Q1 into compilable code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    It's hard to read the code in your post since the formatting/indentation is all screwy. That means it's harder for me to verify, but at a glance:

    1. Wrong. Try stepping through each line carefully by hand. Make a table to track values of x and y if you need to.
    2. Right.
    3. How would you average 10 numbers by hand? If you can't do it by hand, you can't write a program to do it. So pay careful attention to each step you take to average a list of numbers by hand, write them down, and turn each one into the corresponding line(s) of code.
    4. I think you have a typo on line 33, there are two y parameters. In any case, it's simple, you need to call the xyz function and get it's return value. If you don't know how to call a function, or get it's return value, then you best read through your notes/textbook or some tutorials (like the ones on this site).

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    for 1. It would be just thre * vetical correct?


    Quote Originally Posted by anduril462 View Post
    It's hard to read the code in your post since the formatting/indentation is all screwy. That means it's harder for me to verify, but at a glance:

    1. Wrong. Try stepping through each line carefully by hand. Make a table to track values of x and y if you need to.
    2. Right.
    3. How would you average 10 numbers by hand? If you can't do it by hand, you can't write a program to do it. So pay careful attention to each step you take to average a list of numbers by hand, write them down, and turn each one into the corresponding line(s) of code.
    4. I think you have a typo on line 33, there are two y parameters. In any case, it's simple, you need to call the xyz function and get it's return value. If you don't know how to call a function, or get it's return value, then you best read through your notes/textbook or some tutorials (like the ones on this site).

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,877
    Hint: On getting the code to Compile, C does NOT like smart quotes. Use the normal double quote " instead of “ or ”.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    fo number 4, I can't figure out my error. It says my error is in my scanf line, but I don't understand why, any guidance?

    Code:
    #include
    Code:
    "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    int x, y, z;
    int xyz( int x, int y, int z );
    int
     main()
    {
        printf("Enter values for x y z");
        scanf("%d %d %d", &x &y &z);
    return
     0;
     }
     
    int xyz ( int x, int y, int z ) {
     
    int a, b;
     a = x + y + z;
     b = a/3;
     
    return b; }
    
    
    Last edited by jayygerman; 08-22-2013 at 02:46 PM.

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    I do have the #include <stdafx.h" in my program, idk why its not showing on here
    Last edited by jayygerman; 08-22-2013 at 02:46 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    some commas are missing
    kurt

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    nvm, missing comas.

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    Thans kurt, just realized it.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by jayygerman View Post
    for 1. It would be just thre * vetical correct?
    No. I told you it was wrong. Salem gave you one way to find out. I think it's best for you to be the compiler. Learning to read code and understand what it does is a critical skill for any programmer. For example, given the code (properly indented):
    Code:
    int x =5, y;
    while (x >= 2) {
        y=1;
        while (y <= x) {
            printf ( " * " );
            y++;
        }
        x--;
        printf ( "\n" );
    }
    Make a table (I prefer pen and paper, it's quicker) that shows you variables at each step of the program. Go line by line, with a separate entry for each line in the program
    Code:
    Table of program state, line-by-line
    
      x  |  y  | Line | What the code does
    -----+-----+------+--------------------
      5  |  ?  |   1  | Initializes x to 5, y uninitialized
      5  |  ?  |   2  | Is x >= 2? Yes, enter outer loop
      5  |  1  |   3  | Initializes y to 1
      5  |  1  |   4  | Is y <= x? Yes, enter inner loop
      5  |  1  |   5  | prints " * "
      5  |  2  |   6  | Increments y
      5  |  2  |   7  | Goes to top of inner loop (line 4)
      5  |  2  |   4  | Is y <= x ? Yes, enter inner loop
      5  |  2  |   5  | prints " * "
      5  |  3  |   6  | Increments y
    ...
      5  |  6  |   7  | Goes to top of inner loop (line 4)
      5  |  6  |   4  | Is y <= x ? No, go to end of inner loop (line 8)
      4  |  6  |   8  | Decrement x
      4  |  6  |   9  | prints a new line ("\n")
      4  |  6  |  10  | Go to top of outer loop (line 2)
      4  |  6  |   2  | Is x >= 2? Yes, enter outer loop
    ...
    Fill out the rest of that table, until you are all done looping (the condition on line 2 is false -- x is finally less than 2). Then, look at all the lines that print something, and write them out exactly in the order they happen, spaces, new lines, everything. That is what you need to do here. Yes, it's tedious, but it will soon become second nature and you will be able to track much more complex pieces of code in your head.

    Problem 2 is pretty much the same.
    Last edited by anduril462; 08-22-2013 at 02:53 PM.

  12. #12
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    *****
    ****
    ***
    **
    *

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Closer, but also not quite right. Pay careful attention to what is printed, besides asterisks and a new line. Note, I often post console output in code tags, since it preserves alignment/spacing.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    10
    Sorry man, programming is NOT my thing lol. I'm not a computer science major. I'm really trying but this programming isn't my thing.
    I really don't get it.
    would it print 5x5 square of astricts?

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by jayygerman View Post
    Sorry man, programming is NOT my thing lol. I'm not a computer science major. I'm really trying but this programming isn't my thing.
    I really don't get it.
    would it print 5x5 square of astricts?
    This is not so much about programming as it is about critical thinking and being detail oriented. Your inverted triangle was quite close, but was missing a few spaces here and there.

    EDIT: And programming is nobody's thing the very first time they're introduced to it all. It takes a bit of work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding some code please help
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 09:01 PM
  2. Code trouble
    By frterwil in forum C Programming
    Replies: 4
    Last Post: 02-11-2009, 11:32 AM
  3. having trouble with some code for my class
    By green11420 in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2008, 10:33 AM
  4. Trouble with code, cin not working
    By PenguinCoder in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2005, 03:07 PM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM