re:c++.prog.newbie on CPP board

This is a discussion on re:c++.prog.newbie on CPP board within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void main(void) { char MatrixA[6][12]= {"{ 10, 32, 54, 11 , 99, 11 }", "{ 10, 32, ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    re:c++.prog.newbie on CPP board

    Code:
    #include <stdio.h>
    void main(void)
    {
    	char MatrixA[6][12]=
    		{"{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }"};
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and i=j.\n");
    
    }
    I decided to extend your discussion to help me study matrices. Can you help me figure out why I am getting warnings and errors with this code?

    warnings like:warning C4045: '{ 10, 32, 54, 11 , 99, 11 }' : array bounds overflow
    error: error C2078: too many initializers

    [6][12] appears to be right to me. I also tried [5][11] (as is 0 to 5,0 to 11) and still no luck

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    try [12][6] instead of [6][12] and see if that doesn't do anything.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    im not to sure but i think its supposed to be

    {{10, 32, 54, 11 , 99, 11},
    . . .
    {10, 32, 54, 11, 99, 11}};


    without the ' " ' before and after each one
    Last edited by c++.prog.newbie; 04-25-2004 at 11:21 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > void main(void)
    main returns an int
    int main ( void )
    or
    int main ( int argc, char *argv[] )

    > char MatrixA[6][12]=
    Your strings are like 30+ chars long, and you have 12 of them
    So
    char MatrixA[12][40]=

    Of course, if you're really after an array you could do your matrix operations on, it would be an array of ints, not an array of strings looking like an array of ints
    Code:
    int MatrixA[12][6]= {
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 },
        { 10, 32, 54, 11 , 99, 11 }
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    re:Salem

    Of course, so logical.

    As far as using an array of ints, you are right, I should just probably print the { braces on the screen with a diffferent function or set of commands so that I can use ints.


    I'm going to try using the array bound of 40 like you suggested.

    So far I did change it to
    Code:
    #include <stdio.h>
    void main(void)
    {
    	char MatrixA[12][6]=
    		{"{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }",
             "{ 10, 32, 54, 11 , 99, 11 }"};
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and i=j.\n");
    
    }
    and managed to get rid of the error, but the warnings are still there.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    okay thanks for the help all

    No more errors or warnings and my code is:

    Code:
    #include <stdio.h>
    void DisplayOpenCurlyBrace();
    void DisplayClosingCurlyBrace();
    void main(void)
    {
    	int MatrixA[12][6]=
    		{ 10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
            10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 };
    
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and i=j.\n\n\n\n");
    
    printf("\n\n\n");	
    DisplayOpenCurlyBrace();
    printf("// matrix contents");
    DisplayClosingCurlyBrace();
    printf("\n\n\n");	
    } 
    void DisplayOpenCurlyBrace()
    {
    
    	printf("{");
    }
    void DisplayClosingCurlyBrace()
    {
    	printf("}");
    }

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    At this point I think cpp might be better

    Code:
    #include <stdio.h>
    void DisplayOpenCurlyBrace();
    void DisplayClosingCurlyBrace();
    void main(void)
    {
    	int MatrixA[12][6]=
    		{ 10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 };
    	int row,col;
    
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and j=1.\n\n\n\n");
    
    printf("\n\n\n");	
    DisplayOpenCurlyBrace();
    for (col=0;col<6;col++)
    {
    	
    	for (row=0;row<12;row++)
    	{
    		if (col==5 && row==11)
    			{
    			DisplayClosingCurlyBrace();
    			printf("\n");
    			DisplayOpenCurlyBrace();
    			printf(" %i, ",MatrixA[col][row]);
    			}
    		else printf(" %i, ",MatrixA[col][row]);
    		
    		
    	}
    }
    
    DisplayClosingCurlyBrace();
    printf("\n\n\n");	
    } 
    void DisplayOpenCurlyBrace()
    {
    
    	printf("{");
    }
    void DisplayClosingCurlyBrace()
    {
    	printf("}");
    }
    My OpenCurlyBrace and CloseCurlyBrace functions aren't being called and no newline ... all since I tried to put it in a loop. Could this be why CPP uses constructors and destructors to clear out memory or am I just overlooking something much simpler?


    edwardtisdale.com

    re:nevermind. I think I have the array bounds backwards again. ... not the problem. I'll try return values (below)
    Last edited by edwardtisdale; 04-28-2004 at 09:28 PM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    I still see lots of void main
    You know you can edit your own posts....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    Alright almost got it.
    Code:
    #include <stdio.h>
    //void DisplayOpenCurlyBrace();
    //void DisplayClosingCurlyBrace();
    int main(void)
    {
    	int MatrixA[12][6]=
    		{ 10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 };
    	int row,col;
    
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and j=1.\n\n\n\n");
    
    printf("\n\n\n{");	
    //DisplayOpenCurlyBrace();
    for (row=0;row<=12;row++)
    {
    	for (col=0;col<=6;col++)	
    	{
    		if (col==6)
    			{
    			//DisplayClosingCurlyBrace();
    			printf("}\n{");
    			//DisplayOpenCurlyBrace();
    			
    			}
    		 printf(" %i, ",MatrixA[row][col]);
    		
    		
    	}
    }
    
    //DisplayClosingCurlyBrace();
    printf("\n\n\n");	
    return 1;
    } /*
    void DisplayOpenCurlyBrace()
    {
    
    	printf("{");
    }
    void DisplayClosingCurlyBrace()
    {
    	printf("}");
    }*/

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > for (row=0;row<=12;row++)
    > {
    > for (col=0;col<=6;col++)
    Less than - not less equal

    Example
    for ( row = 0 ; row < 12 ; row++ )

    If you'd run it, you would have seen a skewed table.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    I know

    I know it should be because 0 through 5 is an array bound totalling 6 but for some reason using the <=6 I get output:

    { 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 11,

    instead of

    { 10, 32, 54, 11, 99, 11, 10, 32, 54, 11, 99, 11, 10, 32, 54, 11,
    99, 11, 10, 32, 54, 11, 99, 11, 10, 32, 54, 11, 99, 11, 10, 32,
    54, 11, 99, 11, 10, 32, 54, 11, 99, 11, 10, 32, 54, 11, 99, 11,
    10, 32, 54, 11, 99, 11, 10, 32, 54, 11, 99, 11, 10, 32, 54, 11,
    99, 11, 10, 32, 54, 11, 99, 11,

    you see the }\n{ will print out . The only reason I left the <=12 there is just to se what it would do. It turns out you are right then because with the <=12 also there I get

    { 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }
    { 11, 12, 1, 1245120, 4199717, 1, 4325968, }
    { 4326064,


    The problem you see is
    { 10, 32, 54, 11, 99, 11, }
    { 10, 10, 32, 54, 11, 99, 11, }. I don't see why the 10 is output twice.


    I ran the debugger to step through it and the only way out of it was put the printf outside of the inner loop:
    Code:
    #include <stdio.h>
    //void DisplayOpenCurlyBrace();
    //void DisplayClosingCurlyBrace();
    int main(void)
    {
    	int MatrixA[12][6]=
    		{ 10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 };
    	int row,col;
    
    //	printf("This program will multiply the Matrix A per matrix position such that \n");
    //	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and j=1.\n\n\n\n");
    
    printf("\n\n\n{");	
    //DisplayOpenCurlyBrace();
    for (row=0;row<12;row++)
    {
    	for (col=0;col<=6;col++)	
    		printf(" %d, ",MatrixA[row][col]);
    	if (row==11) printf("}\n");
    	else printf("}\n{");
    }
    
    //DisplayClosingCurlyBrace();
    printf("\n\n\n");	
    return 1;
    } /*
    void DisplayOpenCurlyBrace()
    {
    
    	printf("{");
    }
    void DisplayClosingCurlyBrace()
    {
    	printf("}");
    }*/
    Last edited by edwardtisdale; 04-29-2004 at 12:09 PM.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I don't see why the 10 is output twice.

    Because you are going outside of the array bounds -- in this case it appears to be the first element in the next row of data.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    29
    Got me. I'm working on passing the array to a function that does multiplication OneToOne on the matrix now, since this (the above code) mysteriously is working. Maybe it's just the VC5.0 compiler. Passing arrays is a pain though, maybe I should switch to pointers. Anyway this is my project so no help is asked for this part...yet.




    Okay not such a pain.
    Code:
    #include <stdio.h>
    #include <stdlib.h>// for exit commands
    //void DisplayOpenCurlyBrace();
    //void DisplayClosingCurlyBrace();
    void MultiplyOnetoOne(int Matrix[12][6]);
    int main(void)
    	{
    	int MatrixA[12][6]=
    		{ 10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 ,
              10, 32, 54, 11 , 99, 11 };
    	int row,col;
    	char GoOn=' ';
    
    	printf("This program will multiply the Matrix A per matrix position such that \n");
    	printf("position2_x=position1_x+i and i=1, and position2_y=position1_y+j and j=1.\n\n\n\n");
    
    	printf("\n\n\n{");	
    	//DisplayOpenCurlyBrace();
    	for (row=0;row<12;row++)
    	{
    		for (col=0;col<6;col++)	
    			printf(" %d, ",MatrixA[row][col]);
    		if (row==11) printf("}\n");
    		else printf("}\n{");
    	}
    
    	//DisplayClosingCurlyBrace();
    	printf("\n\n\n");
    	printf("Enter Y-enter to go on.   ");
    	scanf("%c",&GoOn);	
    	if (GoOn=='y' || GoOn=='Y') MultiplyOnetoOne(MatrixA);
    	else exit(1);
    	return 1;
    	} 
    void MultiplyOnetoOne(int Matrix[12][6])
    	{
    	int row=0;int col=0;int product=1;
    	printf("You are in the MultiplyOnetoOne function.\n");
    	while (row<6)
    		{
    		while (col<6)	
    			{
    			product*=Matrix[row][col];
    			if (col==5) printf("%i = %i.\n",Matrix[row][col],product);
    			else printf("%i X ",Matrix[row][col]);
    			row++;col++;
    			}
    
    		}
    	}
    
    
    
    /*
    void DisplayOpenCurlyBrace()
    {
    
    	printf("{");
    }
    void DisplayClosingCurlyBrace()
    {
    	printf("}");
    }*/
    Last edited by edwardtisdale; 04-29-2004 at 10:47 PM.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Code:
    	while (row<6)
    		{
    		while (col<6)
    Just use the same for loop ideas you used to print the array in the first place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a second opinion - code error and i cant see it
    By bigfootneedhelp in forum C Programming
    Replies: 19
    Last Post: 10-25-2007, 06:02 AM
  2. Constructor problem
    By rebel in forum C++ Programming
    Replies: 22
    Last Post: 01-11-2006, 05:45 AM
  3. function trouble
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2005, 04:23 AM
  4. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 06:27 AM

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