Thread: How to print an array? Help PLEASE!!!!

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    How to print an array? Help PLEASE!!!!

    I'm taking an intro to C Programming course and I've never taken any other programming course before this class. Currently we're up to arrays, and haven't gotten any further than that. The assignment can be found http://scifisamurai.tripod.com/lamara_-_prog05.rtf

    I can't seem to figure out how to print the array in the gcc compiler. So far here's my code:
    -----------------------------------------------------------
    #include <stdio.h>

    int main(void)
    {
    int i, k;
    int a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4, a5, b5, c5,$
    int votes[5][5]; /*={{1, a1, b1, c1, d1}, {2, a2, b2, c2, d2}, {3, a3, b3, c3, $
    b4, c4, d4}, {5, a5, b5, c5, d5}};
    */

    votes[0][0]=1;
    votes[0][1]=a1;
    votes[0][2]=b1;
    votes[0][3]=c1;
    votes[0][4]=d1;
    votes[1][0]=2;
    votes[1][1]=a2;
    votes[1][2]=b2;
    votes[1][3]=c2;
    votes[1][4]=d2;
    votes[2][0]=3;
    votes[2][1]=a3;
    votes[2][2]=b3;
    votes[2][3]=c3;
    votes[2][4]=d3;
    votes[3][0]=4;
    votes[3][1]=a4;
    votes[3][2]=b4;
    votes[3][3]=c4;
    votes[3][4]=d4;
    votes[4][0]=5;
    votes[4][1]=a5;
    votes[4][2]=b5;
    votes[4][3]=c5;
    votes[4][4]=d5;

    scanf("%d%d%d%d%d", &votes[0][1], &votes[1][1], &votes[2][1], &votes[3][1], &votes[4][1]);
    scanf("%d%d%d%d%d", &votes[0][2], &votes[1][2], &votes[2][2], &votes[3][2], &votes[4][2]);
    scanf("%d%d%d%d%d", &votes[0][3], &votes[1][3], &votes[2][3], &votes[3][3], &votes[4][3]);
    scanf("%d%d%d%d%d", &votes[0][4], &votes[1][4], &votes[2][4], &votes[3][4], &votes[4][4]);
    printf("Precinct\tCandidate A\tCandidate B\tCandidate C\tCandidate D\n");
    for (i = 0; i < 5; i++)
    for (k = 0; k < 5; i++)
    printf("%d\t", votes[i][k]);
    }

    --------------------------------------------------------

    I used a data file with just 20 random numbers for the 20 variables (a1, b1, etc...) and typed a.out < votes.txt (the name of the data file) and the compiler will print out only Precinct, Candidate A etc... but won't print out the rest instead after printing out the Precinct, etc... it says "Segmentation Fault". I looked up what that meant on google and I guess there's something wrong with my array assignments or the bus on the compiler (what is that?).

    Any help on this would be MUCH appreciated! I still have the rest of the program to do and I just can't figure this part out. Thanks in advance!

  2. #2
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Smile SEE ?? for corrections!!!!

    Code:
    int main(void)
    {
    int i, k;
    int a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4, a5, b5, c5,$  // missing semicolon at end and missing d5
    int votes[5][5]; /*={{1, a1, b1, c1, d1}, {2, a2, b2, c2, d2}, {3, a3, b3, c3, $
    b4, c4, d4}, {5, a5, b5, c5, d5}};
    */
    here is a cou[le of mistakes to start with.
    late,
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Aside from the fact that that is horrificly ugly code, you're missing a line of scanf, since you never scan anything into the array[0][0] - array[0][4] values.

    Ignoring all of that, the lines where you assign values implicitly to the array are of no use, since your variables don't contain any initialized value. (IE: They're going to contain random garbage.)

    Furthermore, what's with the $ sign on the end of the lines there? I'll assume it's your text editor breaking...

    Try printing small arrays first, see my example in this thread.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Your code makes my head hurt and requires a rethink.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 01:41 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 10
    Last Post: 07-13-2003, 01:48 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM