Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5848851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:55:02+00:00 2026-05-22T12:55:02+00:00

Learning C and this is an exercise. The user is asked to input the

  • 0

Learning C and this is an exercise.

The user is asked to input the dimension of an two-dimensional array.
Then the user inputs the values of that array.
Finally, the program must be able to sort every line from the lowest to the greatest and print the result.

For instance

input:

2 2
13 11
13 11

Should print:

11 13
11 13

But my program is printing:

11 13
13 11

If in my code I declare a fixed array size, lets say matrix[2][2] and change all the code to that array dimension, the program works great but if I declare matrix[MAX][MAX], since I don know what would be the size of the array, it gives me the output I’ve posted above.

Is this because I’m not using pointers and memory allocation? (Remember that I’m still learning C).

Here is the code I’ve done:

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000

/*Print the final two-dimensional array*/
void print( int a[MAX][MAX], int b[])
{
    int i,j;
    for (i=0; i<b[0]; i++)
    {
        for (j=0; j<b[1]; j++)
        {
            printf("%d", a[i][j]);
            if (j < b[1]-1)
            {
                printf(" ");
            }
        }
        printf("\n");
    }
}

/* Sort my array */
int Qsort (int v[MAX][MAX], int li, int ls)
{
    int j;
    if (li < ls)
    {
        j = order(v,li, ls);
        Qsort (v, li, j-1);
        Qsort (v, j+1, ls);
    }
    return v[MAX][MAX];
}


int order(int x[], int li, int ls)
{
    int a, down, up, temp;
    a=x[li]; down=li; up=ls;
    while (down<up)
    {
        while (x[down]<=a && down<ls)
            down ++;
        while (x[up]>a)
            up --;
        if (down < up)
        {
            temp=x[down];
            x[down]=x[up];
            x[up]=temp;
        }
    }
    x[li]=x[up];
    x[up]= a;
    return up;
}

int main()
{
    int i,j,size,jump,jump2;
    int tam[2];
    int matrix[MAX][MAX];

    /*Define rows and columms*/
    i=0;
    while (i<2)
    {
        scanf("%d", &tam[i]);
        i++;
    }

    /*Fill the array with the numbers the user inputs*/
    for (i=0; i<tam[0]; i++)
    {
        for (j=0; j<tam[1]; j++)
        {
            scanf("%d", &matrix[i][j]);
        }
    }

    size = tam[1];
    jump = 1;
    jump2 = 0;

    for(i=0;i<tam[0];i++)
    {
        Qsort (matrix,jump2, jump*size-1);
        jump++;
        jump2 = jump2 + size;

    }

    print (matrix,tam);

    return 0;
}

Thanks,

Favolas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:55:03+00:00Added an answer on May 22, 2026 at 12:55 pm

    It looks like you are sorting the first row over and over. You only want to pass a single column to Qsort so it should be declared:

    Qsort(int v[MAX], int li, int ls)
    

    In your main function, simply call Qsort with each successive row:

    Qsort(matrix[i], 0, size-1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning ASP.NET MVC Framework, From some articles like this , it seems that
query level: beginner As part of a learning exercise I have written code that
Im learning lisp and im pretty new at this so i was wondering... if
I'm learning objective-C and Cocoa and have come across this statement: The Cocoa frameworks
I'm still learning ASP.NET and I often see code like this throughout parts of
This is going to sound like a silly question, but I'm still learning C,
I have recently started learning F#, and this is the first time I've ever
I am learning Ruby on Rails so I'm sure I'll find this out sooner
I'm learning about POSIX threads right now, but I guess this is just a
I was just reading through Learning Python by Mark Lutz and came across this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.