User defined number of columns?

This is a discussion on User defined number of columns? within the C Programming forums, part of the General Programming Boards category; How would you go about assigning a table of data a specific number of columns, by a user defined function? ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    6

    User defined number of columns?

    How would you go about assigning a table of data a specific number of columns, by a user defined function?

    for example if you had:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int columns, i;
    
    int main()
    {
    
    printf("\nEnter number of columns [1 to 10]: ");
    scanf("%d", &columns);
    
    for (i=0; i<15; i++)
    printf("%d ", i);
    
    }

    How would you modify this so that the variable 'columns' was the amount of entries in the line before a new line began. So say you entered 8, the output would be..

    0 1 2 3 4 5 6 7
    8 9 10 12 13 14

    Thanks in advance!

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    1. Create a 1d array with as many rows as you want. Make it an array of pointers.
    2. Dynamically make a new array by using the malloc() function and put the address of it in the first element in the array of pointers.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I think he just wants to print some numbers in a given number of columns, not create a new data structure.

    It's best not to use global variables unless there's a good reason.

    Columnation can be implemented like this:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int columns, n, i;
    
        printf("Columns: ");
        scanf("%d", &columns);
    
        printf("Numbers: ");
        scanf("%d", &n);
    
        for (i = 0; i < n; i++) {
            printf("%3d ", i);
            if ((i + 1) % columns == 0 && (i + 1) < n)
                putchar('\n');
        }
        putchar('\n');
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined Header
    By $l4xklynx in forum C++ Programming
    Replies: 33
    Last Post: 12-01-2008, 06:09 PM
  2. User-defined functions
    By naya22 in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2007, 12:25 PM
  3. Creating a user defined number of variables
    By beanroaster in forum C++ Programming
    Replies: 10
    Last Post: 09-14-2005, 04:53 PM
  4. user-defined functions
    By MyntiFresh in forum C++ Programming
    Replies: 22
    Last Post: 07-10-2005, 02:11 PM
  5. Number of columns in ListControl
    By MPSoutine in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2003, 02:29 PM

Tags for this Thread


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