Thread: Array help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Array help

    I'm doing some practice hw and I'm stuck on two of them, anyone care to help?


    1. A two dimensional array has 5 rows and 5 columns. Assume the array has been initialized. Write C code to sum the first and third columns. Write a C statement to sum the diagonal elements from bottom left to top right.

    2. For the double array z, write the code to sum the elements 7 through 16 of the array and print the resultn. Assume: double z and that the array has already been initialized.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'm doing some practice hw
    If you really doing it - you have some code
    at least

    Code:
    int main(void)
    {
       return 0;
    }
    Show what you have and explain what are the problems you cannot resolve
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    its all hand written hw, i understand that for a two dimensional array its [][] but i do not know how to sum the separate columns without summing the entire program

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    its all hand written hw
    I have the answer, but its on a piece of paper. You can come over to my house and collect it

    Basically, type out what you have written, test it, and then ask for help if it does not work. Explain how does it not work, and post the code you tested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(x=0;x<5;x++)
       sum += arr[x][3];
    something like that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Well for the second one i have


    int I, x[20], total=0;
    For (I=6; I<=15, I++)
    total=total+x[I];
    printf ("%1d/n",x[I])

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int b, c;    
            float a[5][5];     
    
            //'init array a
            for ( c=0; c < 5; c++)
            {       
                    for ( b=0; b<5; b++)
                    {
                            a[b][c] = 1;
                    }
            }
     
    
            return 0;
    }
    what else do you have coded?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use code tags, please.
    Secondly, x is not initialized. Even though it says assume, you can't run this code. If you already have some code where it's initialized, that's fine.
    Code:
    total=total+x[i];
    Is the same as
    Code:
    total += x[i];
    Code:
    printf ("%1d/n",x[i])
    %1 is not a format specifier. If you want to pad with 0s, perhaps something like
    Code:
    printf ("%0.1d\n",x[i])
    NOTE: Unsure on this one.

    Also, '/n' is not what you expect.
    It's '\n'. Forget Linux-type paths. '\n' is not a path, it's a special char and special chars always begins with \.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM