Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C array print layout

  • 25-02-2009 10:40pm
    #1
    Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭


    hey i have a simple C program that i want to ask the user how many array elements they want from the keyboard, then randomly put values into each array element, then print the values of the array in groups of 5.

    example output would be:
    enter in amount of array elements: 7
    
    01 02 03 04 05
    
    06 07
    
    enter in amount of array elements: 10
    
    01 02 03 04 05
    
    06 07 08 09 10
    
    enter in amount of array elements: 12
     
    01 02 03 04 05
     
    06 07 08 09 10
    
    11 12
    

    i'd like to have the amount of elements that are on a row taken in from the keyboard so the user has complete control over how many elements and how many per row are displayed.

    here's what i've got at the moment (not all code is here like #includes etc that aren't relevant to the problem).
    int num_elements = 0;
    int num_elements_row = 0;
    
    printf("please enter the amount of elements you want: ");
    scanf("%d", &num_elements);
    
    printf("\nplease enter the amount of elements you want on each row: ");
    scanf("%d", &num_elements_row);
    
    int array[num_elements];
    
    int i = 0;
    
    for(i = 0 ; i < num_elements; i++)
    {
         array[i] = rand()%1000;
    }
    
    //now to attempt printing out the elements in their rows.
    for(i = 0 ; i < num_elements; i++)
    {
        printf("%d ", array[i]);
    }
    

    now the last for loop will print out the elements all on one line no problem but i'm having difficulty visualising how i'd get it to skip to a new line after the num_elements_row limit is hit.


Comments

  • Registered Users, Registered Users 2 Posts: 2,534 ✭✭✭FruitLover


    Use the counter variable in the output loop. If it (plus 1) is cleanly divisible by num_elements_row, then bump.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Continuing from what FruitLover said...

    Haven't tested anything but could use modulo function:
    //now to attempt printing out the elements in their rows.
    for(i = 0 ; i < num_elements; i++)
    {
        printf("%d ", array[i]);
        if ((i +1) % num_elements_row == 0)
          printf("\n ");
    }
    


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    thanks guys, i forgot about the modulus operator :o.

    ya should have seen some of the long winded code involving a counter here, a counter there, a counter everywhere to try and solve this.


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    While im in here - cant remember how to search text file for char ' - whats the syntax?? Below for colon but does complier accept ''' ??

    if (a_single_character == ':'){
    colon = colon + 1;
    a_single_character = (char)fgetc(fp);


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    You have to escape it, ie. \'.

    So
    if (a_single_character == '\'')
    { ...
    


  • Advertisement
  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Youre a Gentleman - thanks


  • Closed Accounts Posts: 60 ✭✭LARDO


    u could have a 2 dimensional array!


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    another problem but unrelated and i don't want to start another thread.

    lets say i have an array of size 20.

    int array[20];

    to which i insert a random number into by the following.
    for(i = 0 ; i < 20 ; i++)
              array[i] = rand()%100;
    
    which will put a random number into each element. this works fine, but there are certain numbers that may pop up for example 0, 1, 2, 3 that i do not want to be put in the array. but i need all elements filled with a number between 4-100 and no duplicate values if possible


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    another problem but unrelated and i don't want to start another thread.

    lets say i have an array of size 20.

    int array[20];

    to which i insert a random number into by the following.
    for(i = 0 ; i < 20 ; i++)
              array[i] = rand()%100;
    
    which will put a random number into each element. this works fine, but there are certain numbers that may pop up for example 0, 1, 2, 3 that i do not want to be put in the array. but i need all elements filled with a number between 4-100.
    for(i = 0 ; i < 20 ; i++)
              array[i] = rand()%96 + 4;
    


  • Registered Users, Registered Users 2 Posts: 6,465 ✭✭✭MOH


    which will put a random number into each element. this works fine, but there are certain numbers that may pop up for example 0, 1, 2, 3 that i do not want to be put in the array. but i need all elements filled with a number between 4-100.

    rand()%96 + 4 will give you 4 to 99
    or ran()%97 + 4 for 4 to 100, inclusive


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    MOH wrote: »
    rand()%96 + 4 will give you 4 to 99
    or ran()%97 + 4 for 4 to 100, inclusive
    Sorry, typo - MOH is correct.
    (rand()%(maximum-minimum))+minimum
    


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    thanks guys :pac:


Advertisement