Thread: Printing Array help

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    63

    Printing and Modifying Array help

    My new problem:
    Ok, I think I am doing this right. How do I get it to print the new array after I have changed the element to the guest initial?

    EDIT: Also I know that I am probably not doing this the best way possible, but I have to do it the way I know how, I want to understand what I am doing, and this is just the way I work it out.

    This is what I have:

    Code:
    #include <stdio.h>
    #define ROWS 4
    #define COLUMNS 2
    
    #include <stdio.h>
    
    void printArray(int a[][2]);
    void modifyElement(int e, char guest);
    
    int main()
    {
    	int table[ROWS][COLUMNS]={1,2,3,4,5,6,7,8};
    	int seat;
    	int r;
    	int c;
    	char guest, junkChar;
    
    	printArray(table);
        getchar();
    
    	printf("In which seat would you like to place a guest?\n");
    	scanf("%d",&seat);
    
    	scanf("%c",&junkChar);
    	printf("What is the initial of the guest you would like to place in this seat?\n");
    	scanf("%c",&guest);
    
    	switch(seat){
    	case 1:
    		r=0;c=0;
    		break;
    	
    	case 2:
    		r=0;c=1;
    		break;
    	
    	case 3:
    		r=1;c=0;
    		break;
    	
    	case 4:
    		r=1;c=1;
    		break;
    	
    	case 5:
    		r=2;c=0;
    		break;
    
    	case 6: 
    		r=2;c=1;
    		break;
    
    	case 7:
    		r=3;c=0;
    		break;
    
    	case 8:
    		r=3;c=1;
    		break;
    
    	default:
    		printf("You have entered an invalid seat number, please restart program.\n");
    
    		return 0;
    		break;
    	}
    	
    	modifyElement(table[r][c],guest);
    
    	
    	return 0;
    
    }
    
    void printArray(int a[][2])
    {
    	int i;
    	int j;
        printf("\t_____U____\n");
    	for(i=0;i<=3;i++) {
    
    		for(j=0;j<=1;j+=2){
    			printf("\t%d|\t|%d\n",a[i][j],a[i][j+1]);
    		}
    
    	}
    	printf(" \t----------\n");
    }
    
    void modifyElement(int e, char guest)
    {
    	
    	e = guest;
    	
    }


    ORIGINAL POST (already figured out):
    How would I write a function to print an array that looks like this:

    __U__
    1| |2
    3| |4
    5| |6
    7|___|8

    I am able to get the array, but dont know how to get the lines.

    Thanks in advance for any input.

    EDIT: Its actually supposed to look like a rectangular table with columns 1,3,5,7 and 2,4,6,8. I dont know why I cant make it look right in this post.
    Last edited by saahmed; 02-22-2006 at 09:46 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Maybe use formatting tags

    Code:
     __U__
    |1 |2 |
    |3 |4 |
    |5 |6 |
    |7 |8 |
    This shouldn't be very difficult if you know how printf() works. Here is an example of one of your lines:

    Code:
    printf(" __U__ ");
    printf("|%d |%d |", var[0][0], var[0][1]);
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Yeah, printf would be very simple. But I am required to write a function to print out the array. I am able to get the array, I just dont know how to format it in order to look like a rectangular dinner table. (the lines are the table, the numbers are the seat numbers).

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're not being clear at all as to what you need to do.

    Is this a character array that you need to add lines and spaces to?

    Is it integers that you just need to output in a function?

    Did you make any attempt at this, yet?

    Just because you teacher says put it in a function doesn't mean you don't use standard output functions. You have to tell me what the exact assignment is. For all I know, you have an integer array that you just need to make a function that loops through the array and outputs it into a table.
    Last edited by SlyMaelstrom; 02-22-2006 at 01:31 AM.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    5
    ya i agree with SlyMaelstrom. You should be more clear with what you want to do, shouldn't ya?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    This is the full assignment, so far I am working on the first part.

    Code:
    You are having a dinner party of 8 guests at your mansion, and are employing your massive computer skills to figure out where to seat people. You will sit at the head of the table with four people on each side. 
    
    Your guests include:
    Ms. Peacock 			P
    Mr. Green 			G
    Professor Plum		L
    Colonel Mustard		M
    Ms. Scarlet			S
    Professor Thomas		T
    Professor Braun		B
    Professor Robinson		R	
     __U__
    1|	|2
    3|	|4
    5|	|6
    7|____ |8
    Example	
    
    Ms. White will be serving dinner, of course.
    
    You know that Professor Plum and Colonel Mustard will start arguing about the necessity of commenting code if they sit next to or directly across from each other.
    
    You also know that Ms. Scarlet once copied a program of Ms. Peacock’s. As a result they cannot sit next to or across from each other without arguing.
    _______________________________________________________________________
    
    Use a FUNCTION to print out the ARRAY with seat number as shown above. 
    
    Then use a FUNCTION to allow the user to call a chair and to place a person’s initial in that position in the ARRAY. Use your ARRAY printing function to print out the seating arrangement after each person is seated as well as at the end of the program. 
    
    Write a FUNCTION specific for this example to check to see if people will argue if next to each other. If the person cannot sit where placed without arguing, inform the user and end the program (you may check after each placement or at the end). Assume all entries will be uppercase.
    One more thing,
    USE AN ARRAY
    
    Hint: x= =’m’ may be used for character comparisons.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    this is what I have so far.

    Code:
    #include <stdio.h>
    
    void printArray(const int a[][2]);
    int main()
    {
    	int table[4][2]={1,2,3,4,5,6,7,8};
    
    	printArray(table);
    
    	return 0;
    
    }
    
    void printArray(const int a[][2])
    {
    	int i;
    	int j;
    
    	for(i=0;i<=3;i++) {
    
    		for(j=0;j<=1;j++){
    			printf("%d",a[i][j]);
    		}
    
    		printf("\n");
    	}
    }

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, i have got you the output u have think about the exchange of the elements in the array according to your sitting arrangment
    Code:
    #include <stdio.h>
    
    void printArray(const int a[][2]);
    int main()
    {
    	const int table[4][2]={1,2,3,4,5,6,7,8};
    
    	printArray(table);
        getchar();
    	return 0;
    
    }
    
    void printArray(const int a[][2])
    {
    	int i;
    	int j;
        printf("\t_____U____\n");
    	for(i=0;i<=3;i++) {
    
    		for(j=0;j<=1;j+=2){
    			printf("\t%d|\t|%d\n",a[i][j],a[i][j+1]);
    		}
    
    	}
    	printf(" \t----------\n");
    }
    /* myoutput
            _____U____
            1|      |2
            3|      |4
            5|      |6
            7|      |8
            ----------
    */
    ssharish2005

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you bothering with a two dimensional array? Really, why are you even bothering with seating at all?
    Ms. White will be serving dinner, of course.

    You know that Professor Plum and Colonel Mustard will start arguing about the necessity of commenting code if they sit next to or directly across from each other.

    You also know that Ms. Scarlet once copied a program of Ms. Peacock’s. As a result they cannot sit next to or across from each other without arguing.
    Just stick them all in the Conservatory with a lead pipe and a candle stick, and soon all you'll have to seat is one person.


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

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Not unless the police arrive, and unfortunately, Mr Body never called them.
    Sent from my iPad®

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by quzah
    Why are you bothering with a two dimensional array? Really, why are you even bothering with seating at all?Just stick them all in the Conservatory with a lead pipe and a candle stick, and soon all you'll have to seat is one person.


    Quzah.
    LOL. I wish it was that wasy.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    Quote Originally Posted by ssharish2005
    well, i have got you the output u have think about the exchange of the elements in the array according to your sitting arrangment
    Code:
    #include <stdio.h>
    
    void printArray(const int a[][2]);
    int main()
    {
    	const int table[4][2]={1,2,3,4,5,6,7,8};
    
    	printArray(table);
        getchar();
    	return 0;
    
    }
    
    void printArray(const int a[][2])
    {
    	int i;
    	int j;
        printf("\t_____U____\n");
    	for(i=0;i<=3;i++) {
    
    		for(j=0;j<=1;j+=2){
    			printf("\t%d|\t|%d\n",a[i][j],a[i][j+1]);
    		}
    
    	}
    	printf(" \t----------\n");
    }
    /* myoutput
            _____U____
            1|      |2
            3|      |4
            5|      |6
            7|      |8
            ----------
    */
    ssharish2005

    Thanks, this makes sense. Now I have to work on the hard part.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    63
    I am mssing around with modifying one element of the array, and its not working out. I am just getting a junk number. What am I doing wrong? I am using a function modifyElement, the actual element is not being passed. I am thinking it has something to do with the int e.

    Code:
    #include <stdio.h>
    #define ROWS 4
    #define COLUMNS 2
    
    #include <stdio.h>
    
    void printArray(int a[][2]);
    void modifyElement(int e);
    
    int main()
    {
    	int table[ROWS][COLUMNS]={1,2,3,4,5,6,7,8};
    
    	printArray(table);
        getchar();
    
    	modifyElement(table[4]);
    
    	return 0;
    
    }
    
    void printArray(int a[][2])
    {
    	int i;
    	int j;
        printf("\t_____U____\n");
    	for(i=0;i<=3;i++) {
    
    		for(j=0;j<=1;j+=2){
    			printf("\t%d|\t|%d\n",a[i][j],a[i][j+1]);
    		}
    
    	}
    	printf(" \t----------\n");
    }
    
    void modifyElement(int e)
    {
    	int newValue;
    
    	newValue= e*2;
    	printf("Value by 2 is %d\n",newValue);
    }
    EDIT: nevermind, played around with it and figured it out.
    Last edited by saahmed; 02-22-2006 at 07:44 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, your initialization of the array leaves a lot to be desired. Furthermore, why are you even bothering with a multidimension array? One will do just fine:
    Code:
    for( x = 0; x < sizeof( array ) / sizeof( array[ 0 ] ); x++ )
    {
        draw left wall
        display number
        if x is odd
            draw right wall and newline
    }
    if x is odd then we have ended with an uneven setting
        draw right wall, space, right wall, newline
    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Read this. I don't think you're supposed to post your entire assignment. If you have problems, program crashing, etc. don't be afraid to ask. But what's the point of a homework assignment if other people do it for you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing half a char array
    By theoneandonly in forum C Programming
    Replies: 19
    Last Post: 11-11-2006, 07:27 AM
  2. Replies: 3
    Last Post: 11-03-2003, 08:55 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM