Thread: Need Help With Putting Array in columns! any help will be appreciated!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Angry Need Help With Putting Array in columns! any help will be appreciated!

    okay so i have a program that scans in up to 100 integers into an array "myList" now i must write a function so that when i run the program it will print the array in columns of 4 sequentially from left to right and then to row 2 and so on...

    i tried to use a multi-dimensional array and nested for loops but i'm not getting anything here!

    any help would be very appreciated! examples welcomed!

    for more descriptions. when you run the program you can enter up to as many integers up to 100 and will cancel the entry with a letter entry. So if they are scanned into the array myList[size] and size is incrimented with every inputed integer to keep track of how many numbers were saved.

    so my function definition (to organize the entries in rows and columns is set up like so)
    void myPrint(int x[], int size)
    {
    (and i need help writing this function)
    }
    Last edited by Ol_denjin; 10-23-2009 at 11:06 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Loop is a good idea. Ask yourself this question: after which elements do you want to print a new line? What is common about all those numbers?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So if i was your index to the array, and you're printing it in a for loop like so:

    Code:
    for(i = 0; i < maxElements; ++i)
      printf("%d", grid[i]);
    what would i % 4 be equal to, when i was at say, 4?
    Could you use that mod operator, in an if statement, inside the for loop?

    Post up your try! (use code tags: the # icon in the advanced text window) to post your code.

    And welcome to the forum.

    P.S. You can print the array in the format you want, having it as a 2D array, or just 1D. makes no difference, it can all look the same, by a small tweak in the code. Have your array in the number of dimensions you want it to be in, for your programs ease of use and clarity.
    Last edited by Adak; 10-23-2009 at 11:13 AM.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Loop is a good idea. Ask yourself this question: after which elements do you want to print a new line? What is common about all those numbers?
    here's what i'm trying to go with...but it prints out blank rows and columns
    Code:
    void myPrint(int x[], int size)
    {
    	int Print[25][4],i,j;
    
    	for(i=0; i < size; i++)
    	{
    		for(j = 0; j < 4; j++)
    		{
    			Print[i][j] = x[i];
    			printf("\n");
    		}
    	}
    		
    
    	
    }

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Thank you for the welcome Adak...yah i just started this 8 week advanced C course...and it was a year ago when i took the basic C course so i'm kind of losing it aha.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Size should be the number of rows, as you've coded it up, here. (outer loop represents the rows, inner loop represents the columns in a row).

    Since you haven't put anything inside Print[][] array, I'm not sure you'll get much out.

    Don't you want to do something with x[]?

    You don't code in C frequently, you lose it, C? I know the feeling, believe me.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Quote Originally Posted by tabstop View Post
    Loop is a good idea. Ask yourself this question: after which elements do you want to print a new line? What is common about all those numbers?
    so i want to print a new line after the 4th element in each row...and the only thing in common about those numbers are that they were all scanned into my array myList.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Angry

    Quote Originally Posted by Adak View Post
    Size should be the number of rows, as you've coded it up, here. (outer loop represents the rows, inner loop represents the columns in a row).

    Since you haven't put anything inside Print[][] array, I'm not sure you'll get much out.

    Don't you want to do something with x[]?

    You don't code in C frequently, you lose it, C? I know the feeling, believe me.
    lol good point okay...so this was my next attempt.

    Code:
    void myPrint(int x[], int size)
    {
    	int i,j;
    
    	for(i=0; i < size; i++)
    	{
    		for(j=0; j<4; j++)
    
    			printf("%d\n", x[i]);
    		
    	}
    		
    
    	
    }
    and its printing in one single column now and printing every input 4 times lol arrrrghhh

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Ol_denjin View Post
    so i want to print a new line after the 4th element in each row...and the only thing in common about those numbers are that they were all scanned into my array myList.
    Now you'll get the math heads all grumpy!

    Some numbers are even, some numbers are odd, some are prime, some are "happy".

    some are evenly divisible by 4.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're trying to print a 1D array, like it was a 2D array. Remove the inner loop, for 1D arrays.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    lol grumpy mathheads

    sound like a prof. i used to have

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I still recall my geometry teacher - we called him "Mad Dog Cameron".

    He'd get on a roll, and he was the original (and real) "foamer"!

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    aha...that sounds terrifying!

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He had a few girls (this was high school), in tears, and a few of the boys ready to wipe that foam off from around his mouth, with their knuckles.

    The first week he was all "No question is a dumb question, ask away". After that, he changed to his finger in your face and "Can't you see that the angle of A is going to be the angle of C minus the angle of B - 90?"

    "Can't you see that the hypotenuse is this triangle is ...?"

    Very confrontational and accusatory. After the first week, nobody asked him a damn thing, so we didn't learn much. I learned more geometry tutoring others, than I did in his class, for sure.

    Later, we learned that his class was monitored by somebody from the district, during that first week. After that, it wasn't.

    The final project for his class was 50% of your grade. He gave it out and we were all stunned - nobody had a fukking clue how to do geometry like it required.

    Finally, a Dad who was an engineer, helped our classmates, and *everybody* copied off his proof (using just small differences of course).

    You wonder why public education is in the toilet?
    Last edited by Adak; 10-23-2009 at 11:51 AM.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    so heres where i'm at now

    just getting one single columnn...i know i'm missing something simple

    Code:
    void myPrint(int x[], int size)
    {
    	int i;
    	
    
    
    	for(i=0; i < size; i++)
    	{
    		
    			printf("%d", x[i]);
    		printf("\n");
    		
    	}
    		
    
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM