Like Tree1Likes
  • 1 Post By SirPrattlepod

Array and Matrix check pls

This is a discussion on Array and Matrix check pls within the C Programming forums, part of the General Programming Boards category; 1. Input an dimension and elements of an array from the keyboard. Count the odd elements of an array and ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    Question Array and Matrix check pls

    1. Input an dimension and elements of an array from the keyboard. Count the odd elements of an array and that number join to variable K, then sort the last K elements in decreasing order. (I hope you understand what I'm doing here cause I'm not used to type this type of problems in English)

    Code:
    #include <stdio.h>
    main ()
    {
    	int A[100], i, n, j, K = 0;
    	printf ("Type in the dimension of an array: \n");
    	scanf ("%d", &n);
            printf ("Type in the elements of an array: \n");
    	for (i = 0; i < n; i++)
    	{
    		scanf("%d", &A[i]);
    	}
    
    
    	for(i=0; i<n; i++)
    	{
    		if(A[i]%2 == 1) 
            {            
              K++ ; 
              else printf ("There is no odd elements! \n") 
            }
           
    	}
          for(i = n-K; i < n - 1; i++) 
          {
            for(j = i + 1; j < n, j++) 
             {
              pom = A[i];
              A[i] = A[j];
              A[j] = pom;
             }
          }
    2. Input a matrix A(nxm) from a keyboard. Find a column with a minimum and maximum sum of elements and replace their elements.

    Code:
    #include <stdio.h>
    main ()
    {
    int i, j, m, n, A[20][20], sum=0, min, max, index_min, index_max, pom;
    
            printf ("Type in the dimension of a matrix: \n");
    	scanf ("%d%d", &n, &m);
            printf ("Type in the elements of a matrix: \n");
            for (i = 0; i < n; i++)
            {
               for (j = 0; j < m; j++)
    
                 {
                  scanf ("%d", &A[i][j]);
                  }
             }
    for(j = 0; j < n; j++)
    {
     for(i = 0; i < m; i++)
     {
      sum[j] += A[i][j]; 
     }
    }
    
    
    for(j = 0; j < n; j++) 
    {
     sum[0] = max; 
     if(sum[j] > max) 
    {
      max = sum[j]; .
      index_max = j; 
     }
    }
    
    
    for(j = 0; j < n; j++) 
    {
     sum[0] = min;
     if(sum[j] < min)
     {
      min = sum[j];
      index_min = j;
     }
    for(i = 0; i < m; i++)
    {
     pom = A[i][index_max];
      A[j][index_max] = A[j][index_min];
      A[j][index_min] = pom; 
    }
    printf (Transformed matrix is: \n");
    for (i = 0; i < n; i++)
            {
               for (j = 0; j < m; j++)
                 {
                  scanf ("%d", A[i][j]);
                  }
             }
    
    
    
    }
    Can you check for mistakes and help me correct it pls. Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Quote Originally Posted by Crni An View Post
    Can you check for mistakes and help me correct it pls. Thanks!
    Yes, we can. But that's not what we are really here for, to be the internet's QA department. We generally help people fix their problems. That means you should tell us what the problems are.

    But it appears you don't even know if there are any problems. So start by testing your programs. Pick some simple input examples, determine what the output should be (by hand -- paper and pencil perhaps), and then compare. If it doesn't match, try to figure it out, and if you're stuck, post back here with a thorough description of the problem. Note, the first step is to make sure you compile with warnings turned all the way up, and fix all errors and warnings. A compiler warning often indicates a potential bug. If you have warnings/errors you don't understand, copy-paste them here (including line numbers). Other information that helps us: does it produce no output or incomplete output? does it crash (if so, do you get any message about the crash)? does it produce incorrect output? In each of these cases, tell us what input you provided that caused that output. In the last case (incorrect output), please tell us what output you expected to see and what you actually saw.

    All this makes our job of helping you much easier and more effective, and it keeps us happy. Keeping us happy keeps you and your code happy.

    EDIT: Also, fix your indentation (link). If it's hard to read, it's easy to make mistakes, but hard to find or fix them. Plus, it makes our eyeballs hurt. Lastly, I just noticed your code wont compile at all. I think you posted incomplete main functions. Also, review a short tutorial on if/else syntax.
    Last edited by anduril462; 09-24-2013 at 05:26 PM.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    On the second one, when I debug it, it stops at this line
    Code:
    pom = A[i][index_max];
    
    and it says
    'grof.exe': Loaded 'C:\Users\User\Documents\Visual Studio 2010\Projects\grof\Debug\grof.exe', Symbols loaded.
    'grof.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
    'grof.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
    'grof.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
    'grof.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.

    I managed to get the first one working.
    Tnx for response.
    Last edited by Crni An; 09-24-2013 at 06:37 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Please provide the inputs you gave your program so I can replicate your problem.

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    if(sum[j] > max)

    How's that going to work when max is not initialised?

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Ok, if my matrix is 3x4 (n=1, m=4) and it looks like
    1 2 3 4
    5 6 7 8
    9 8 7 6

    so the first column has max sum and second has min sum
    transformed matrix should be
    2 1 3 4
    6 5 7 8
    8 9 7 6
    Srry if didn't understand you quite well...

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    I think I understand what it should do now, but your explanation is full of problems.
    Ok, if my matrix is 3x4 (n=1, m=4) and it looks like
    So you say it's 3x4, but you tell me n=1 and m=4, which would suggest it's 1x4. But then:
    1 2 3 4
    5 6 7 8
    9 8 7 6
    You give a 4x4 matrix! What's going on here? That's doesn't help much at all. What are the actual dimensions and the actual data?

    So you explain with more words:
    so the first column has max sum and second has min sum
    transformed matrix should be
    That would be fine, but the first and second column both have sums of 16. The third column has a sum of 17 and the fourth column a sum of 18. Column 2 is one of the minimum-sum columns, but column 1 is definitely not​ the maximum-sum column. So again, something is very wrong, and you need to clear it up.

    That being said, SirPrattleprod has a good point (and in fact found a definite problem with your code (though there may be others). To explain his post: max is uninitialized, meaning it could have any value, including a very large one. That would mean you never get inside the if on line 29. That means index_max never gets set, so it contains some garbage value. Thus, when you do
    Code:
    pom = A[i][index_max];
    You may be accessing the array out of bounds. You have the same exact problem with min and index_min. Accessing an array out of bounds results in undefined behavior (it's one cause, there are several other causes). That means anything at all could happen; you must avoid undefined behavior if you ever want your programs to work.

    Set max to the smallest possible int value (INT_MIN -- #include <limits.h>) and min to the largest possible int value (INT_MAX). That way, when you compare to find the max and min column sums, you will be sure to find such a column (every column sum will be greater than or equal to the smallest possible value, and less than or equal to the largest possible value), and you will be sure to properly set min, max, index_min and index_max.

  8. #8
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Well, there certainly are other problems. sum is not an array...
    anduril462 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intersection Test Issues (Matrix Calculator Check)
    By MutantJohn in forum C Programming
    Replies: 7
    Last Post: 05-09-2013, 07:33 PM
  2. Parity Check Matrix
    By Ken JS in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2007, 03:51 AM
  3. Replies: 1
    Last Post: 03-08-2006, 05:47 PM
  4. Replies: 1
    Last Post: 03-06-2006, 06:57 PM

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