Jump to content

How can I put a 2d array into a frame?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
blaze505

blaze505

    Newbie

  • Members
  • Pip
  • 2 posts
I am new to C and I am trying to input a 2d array from the user (integers) and want when the program prints the array it would simply be printed with a frame of for example "& or $" any symbol.

This is how the input is:
1 2 3
4 5 6
7 8 9

and the output should be like this:
Code:

        a b c

      & & & & &

    a & 1 2 3 &

    b & 4 5 6 &

    c & 7 8 9 &

      & & & & &


later the input of the user will change and the frame shouldn't change, is there anyway to make two 2d arrays to print into each other, so no matter what i change in one array wont effect the other and they will print this way, assuming the ratio of both arrays will always be this way? I been trying to do this for a while now but nothing is working.( the letters and "&" are the frame) and if the user declares the array size to be bigger than 3 by 3, and inputs it, the frame should be bigger as will.
I would really appreciate it if some1 shows me how this can be done in code.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
This should be fairly straight-forward. What have you attempted so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
blaze505

blaze505

    Newbie

  • Members
  • Pip
  • 2 posts
#include <stdio.h>


int main(){

    int i,j,n;

    char x;

    x = 'a';


    printf("Enter size of array: \n");

    scanf("%d",&n);


    int array[n][n];

    printf("Enter array:\n");

    for(i=0; i < n; i++){

        for(j=0; j < n; j++){

            scanf("%d",&array[i][j]);

        }

    }

    for(i=1;i<(n-1);i++){

        printf(" %c",x);

        x++;

    }


    for(j=1;j<(n-1);j++){

        printf("\n%c ",x);

        x++;

    }

    for(i=0;i<(n+1);i++){

        for(j=0;j<(n+1);j++){

            if (i==1  || j==1 || i == n || j==n){

                printf("& ");

            }

            else if(i==2|| j==2 || i==(n-1)|| j==(n-1)){

                    printf("%d ",array[i][j]);

            }

        }

        printf("\n");

    }


    return 0;

}



This is what i have came up with so far, but its not working at all :S

Edited by dargueta, 28 April 2011 - 11:37 AM.


#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
issues:
1) for the first line of output, you don't want a space before each character, you want two spaces, and then the characters
2) your loop for the first line of output should terminate when i=4, which means your condition should be i<=n
3) for your second output loop, you return a carriage return for EVERY value.

They continue on from there. I would suggest you trace through the code by hand to see what it's doing. That should help make the results make more sense.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,718 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I'd also advise using padding in your printf statements so when you have numbers with different numbers of digits it won't screw up the alignment of your matrix.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users