Thread: Array from 1 to n

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Array from 1 to n

    I'm suppose to make and print an array (it's for a game) for selecting numbers from 1 to n. Like if the number entered is 6, then the numbers in the games are "1 2 3 4 5 6" They are only positive nos, so I can put 0 in the 1st array index so things can be easier to follow, so that 1 is in the 1st, 2 in the 2nd, and n in the nth. But when I use this code, I get that there is a ) missing in the printboard function.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(void) {
    void makeboard(int board[], int size);
    int board[101];
    int size;
    printf ("Enter size:-");
    scanf ("%d", &size);
    makeboard(board,size+1);
    printboard(board,size+1);
    return 0;
    }
    
    
    void makeboard(int board[], int size) {
    int i;
    for (i=0; i<=size; i++) board[0]=i;
    }
    
    void printboard (char board[], int size+1) {
    for (int i=0; i<=size+1; i++) {
    if (board[i]!=0) printf ("%d ", board[i]);}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (int i=0
    This isn't valid C, its C++
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You have quite a few syntax errors and type mismatches. Compare this to what you had before:
    Code:
    #include <stdio.h>
    
    void makeboard(int board[], int size);
    void printboard(int board[], int size);
    
    int main(void) {
      int board[101];
      int size;
      printf ("Enter size:-");
      scanf ("%d", &size);
      makeboard(board,size+1);
      printboard(board,size+1);
      return 0;
    }
    
    void makeboard(int board[], int size) {
      int i;
      for (i=0; i<size; i++) board[i]=i;
    }
    
    void printboard (int board[], int size) {
      int i;
      for (i=0; i<size; i++)
        if (board[i]!=0) printf ("%d ", board[i]);
    }
    My best code is written with the delete key.

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