Thread: short matrix problem

  1. #61
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The c variable in the for loop, only goes from 0 to 9, so 10 rows. It will automatically quit and can't run over that amount, unless you change your code.

    So I don't know what "trick" you're referring to.

  2. #62
    Registered User
    Join Date
    May 2012
    Posts
    210
    guys i have class in an hour I wont bug u again today
    please help me understand how I can exit the program if the user enter more than 10 rows

  3. #63
    Registered User
    Join Date
    May 2012
    Posts
    210
    i want to display an error code myself to the user

  4. #64
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You mean if m > 10?

    You're tough to nail down here.

  5. #65
    Registered User
    Join Date
    May 2012
    Posts
    210
    well yeah if m>10 i can write that down in the beginning
    but what if c >10 (number of rows)

  6. #66
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    C can't be more than 10:

    Code:
    for( c = 0 ; c < 10 ; c++ )

  7. #67
    Registered User
    Join Date
    May 2012
    Posts
    210
    okay thanks

  8. #68
    Registered User
    Join Date
    May 2012
    Posts
    210
    for the EOF when i enter the value of the input and press CTRL-Z the whole program just stops

  9. #69
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kiwi101 View Post
    guys i have class in an hour I wont bug u again today
    Use the time before and after your class to change the variable names.

    I personally won't help you any further if you continue to ignore the advice we give you here.

    Bye, Andreas

  10. #70
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't enter the value, and then enter EOF, at the same time. Enter your values <hit enter>. Next loop, when you want to quit, press the EOF key combo.

  11. #71
    Registered User
    Join Date
    May 2012
    Posts
    210
    I know thats exactly what im doing but its not working
    what if instead of 99 i want to assign a * as end of program how would i do that
    like char *;

    oh and dont worry ill change my variable names

  12. #72
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post your current code, so I can see what's doing.

    Are you testing user's entry for EOF?

  13. #73
    Registered User
    Join Date
    May 2012
    Posts
    210
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int  main()
    {
      int val, m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10]; //defining variables
    
    
      printf("Enter the number of columns of matrix ");
      scanf("%d",&m);
      if(m<=0 || m>10){  //error checking if user enter negative number or 0 for column
        printf("You entered a invalid value.\n");
        exit(0);
      }
       else{
        printf("Enter the elements of matrix \n");
    
    
        for( c = 0 ; c < 10 ; c++) //forst for loop to scan the matrix
           {
        for( d = 0 ; d < m ; d++ )
              {
        scanf("%d",&matrix[c][d]);
                if (matrix[c][d] == 99) // 'x' is character variable I declared to use as a break
                  break;
                if (matrix[c][d] < 0){ //error checking if any entries in the matrix are negative
                  printf ("you entered an invalid number\n");
                  exit(1);}
              }
                if (matrix[c][d] == 99)
                 break;
              }
      }
    
    
      if (d > 0){ //error checking to make sure the number of entries is correct
        printf("The number of entries is not valid\n");
        exit(1);
      }
      printf("\nHere is your matrix:\n");
      int i; //declaring i to be count for row
    
    
      for(i=0;i<c;i++)
        {
      for(d=0;d<m;d++)
            {
    
    
          printf("%3d ",matrix[i][d]);
            }
          printf("\n");
        }

  14. #74
    Registered User
    Join Date
    May 2012
    Posts
    210
    my bad
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int  main()
    {
      int val, m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10]; //defining variables
    
    
      printf("Enter the number of columns of matrix ");
      scanf("%d",&m);
      if(m<=0 || m>10){  //error checking if user enter negative number or 0 for column
        printf("You entered a invalid value.\n");
        exit(0);
      }
       else{
        printf("Enter the elements of matrix \n");
    
    
        for( c = 0 ; c < 10 ; c++) //forst for loop to scan the matrix
           {
        for( d = 0 ; d < m ; d++ )
              {
        scanf("%d",&matrix[c][d]);
                if (matrix[c][d] == EOF) // 'x' is character variable I declared to use as a break
                  break;
                if (matrix[c][d] < 0){ //error checking if any entries in the matrix are negative
                  printf ("you entered an invalid number\n");
                  exit(1);}
              }
                if (matrix[c][d] == EOF)
                 break;
              }
      }
    
    
      if (d > 0){ //error checking to make sure the number of entries is correct
        printf("The number of entries is not valid\n");
        exit(1);
      }
      printf("\nHere is your matrix:\n");
      int i; //declaring i to be count for row
    
    
      for(i=0;i<c;i++)
        {
      for(d=0;d<m;d++)
            {
    
    
          printf("%3d ",matrix[i][d]);
            }
          printf("\n");
        }

  15. #75
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, the check is in there. Did you get it working the way you wanted it to?

    If not, describe when the EOF test should make the entry of numbers, quit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  2. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 11:16 AM
  3. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 12:29 PM
  4. Replies: 2
    Last Post: 04-21-2008, 07:43 PM
  5. Replies: 7
    Last Post: 02-08-2008, 06:31 PM