4 Replies - 1967 Views - Last Post: 16 March 2010 - 01:53 PM Rate Topic: -----

#1 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Sorting in descending order

Posted 16 March 2010 - 12:13 PM

Hi guys.
I am trying to sort a row in a 2D array but it is not work :(
im not sure what is wrong. And i want to sum the entire array.
this is the code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int manipulateArray(int manArray[5][5]);
int printArray(int print[][5]);

int main()
{
//Initializing the array
    int array[5][5] =
    {
        {1,2,3,4,5},
        {6,7,8,9,10},
        {11,12,13,14,15},
        {16,17,18,19,20},
        {21,22,23,24,25}
    };


    printf("********************************************************************************");

    //display the array
    printf("\n\n\t\tThis is the array\n\n\n");
    printArray(array);	//display the array
    printf("\n\n********************************************************************************");
    printf("\n\n");          //print 2 newlines
    manipulateArray(array);  //call the manipulateArray function
    printf("\n");            //print a newline

    getch ();
    return 0;
}


//this function will print the array
int printArray(int print[][5])
{
    int r,c; //declear the row and column counter

    for (r = 0; r <= 4; r++)
    {
        for (c = 0; c <=4; c++)
        {
            printf("\t%d", print[r][c]);
        }
        printf("\n");        //print a newline
    }
}


int manipulateArray(int array[][5])
{
    int temp[5] = {0};

//Reverse the first column
//Print the array on the screen

    printf("The inverse of column 1:\n\n");

//get the numbers
    for (int i = 1; i <= 5; i++)
    {
        temp[i] = array[i][0];
    }

//invert column 1
    for (int i = 0; i < 5; i++)
    {
        static int j = 4;
        array[i][0] = temp[j];
        j--;
        printf("\n%d", temp[j]);
    }

printf("\n\nSwap of column 3 and column 4: \n");

     int temp_1[5] = {0};
    for(int s = 0; s < 5; s++){

    temp_1[s] = array[s][2];//Copy column 3 into a temp_1 array

    array[s][2] = array[s][3];//copy column 4 into column 3

    array[s][3] = temp_1[s]; //copy temp_1 into column 3

    printf("%d  %d", array[s][2], array[s][3]);//print new array

    printf("\n");
    }
    printf("\n\n");

//Sort row 5 in descending order
    printf("Sort of Row 5: \n");
   int pass, i, hold;

    for ( pass = 1; pass < 5; pass++)
    {

    for (i = 0; i < 5 - 1; i++)
    {

    if (array[i] > array[i + 1] ) {
    hold = array[i];
    array[i] = array[i + 1];
    array[i + 1] = hold;
    }
    }
    }



    printf("\n\n");

//Print the sum of the array
    printf("The sum of the array is: \n");
    int c, r;
    int sum = 0;

    for (r = 0; r = 4; r++)
    {
        sum = sum + r;

        for (c = 0; c = 4; c++)
        {
            sum = sum + c;
        }
        printf("%d", sum);

    }
}



this is the part that is giving me the trouble:
//Sort row 5 in descending order
    printf("Sort of Row 5: \n");
   int pass, i, hold;

    for ( pass = 1; pass < 5; pass++)
    {

    for (i = 0; i < 5 - 1; i++)
    {

    if (array[i] > array[i + 1] ) {
    hold = array[i];
    array[i] = array[i + 1];
    array[i + 1] = hold;
    }
    }
    }

what am i doing wrong?

and as i said i also want to sum the array but it is not working either. This is the code i have for the sum:
//Print the sum of the array
    printf("The sum of the array is: \n");
    int c, r;
    int sum = 0;

    for (r = 0; r = 4; r++)
    {
        sum = sum + r;

        for (c = 0; c = 4; c++)
        {
            sum = sum + c;
        }
        printf("%d", sum);

    }


Thank you guys in advance!

Is This A Good Question/Topic? -1
  • +

Replies To: Sorting in descending order

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Sorting in descending order

Posted 16 March 2010 - 12:43 PM

There were so many logical errors in your code, it was disgraceful. Here is the corrected code. Before you simply cut and paste my code and call it your own, I would seriously consider whether or not you are cut out for writing software. Compare my code against yours and learn from your mistakes.

#include <stdio.h> 
#include <conio.h> 
#include <ctype.h> 
 
void manipulateArray(int manArray[5][5]); 
void printArray(int print[][5]); 
 
int main() 
{ 
//Initializing the array 
    int array[5][5] = 
    { 
        {1,2,3,4,5}, 
        {6,7,8,9,10}, 
        {11,12,13,14,15}, 
        {16,17,18,19,20}, 
        {21,22,23,24,25} 
    }; 
 
 
    printf("********************************************************************************"); 
 
    //display the array 
    printf("\n\n\t\tThis is the array\n\n\n"); 
    printArray(array);  //display the array 
    printf("\n\n********************************************************************************"); 
    printf("\n\n");          //print 2 newlines 
    manipulateArray(array);  //call the manipulateArray function 
    printf("\n");            //print a newline 
 
    getch (); 
    return 0; 
} 
 
 
//this function will print the array 
void printArray(int print[][5]) { 
    int r,c; //declear the row and column counter 
 
    for (r = 0; r <= 4; r++) 
    { 
        for (c = 0; c <=4; c++) 
        { 
            printf("\t%d", print[r][c]); 
        } 
        printf("\n");        //print a newline 
    } 
} 
 
 
void manipulateArray(int array[][5]) { 
    int temp[5] = {0}; 
 
	//Reverse the first column 
	//Print the array on the screen 
 
    printf("The inverse of column 1:\n\n"); 
 
	//get the numbers 
    for (int i = 1; i <= 5; i++) { 
        temp[i] = array[i][0]; 
    } 
 
	//invert column 1 
    for (int i = 0, j = 4; i < 5; i++, j--) { 
        array[i][0] = temp[j];  
        printf("\n%d", temp[j]); 
    } 
 
	printf("\n\nSwap of column 3 and column 4: \n"); 
 
    int temp_1[5] = { 0, 0, 0, 0, 0}; 
    for(int s = 0; s < 5; s++) { 
		temp_1[s] = array[s][2];//Copy column 3 into a temp_1 array 
	 
		array[s][2] = array[s][3];//copy column 4 into column 3 
	 
		array[s][3] = temp_1[s]; //copy temp_1 into column 3 
	 
		printf("%d  %d", array[s][2], array[s][3]);//print new array 
	 
		printf("\n"); 
    } 
    printf("\n\n"); 
 
	//Sort row 5 in descending order 
    printf("Sort of Row 5: \n"); 
    int pass, i, hold; 
 
    for ( pass = 1; pass < 5; pass++) { 
 
		for (i = 0; i < 5-pass; i++) { 	 
			if (array[i][4] < array[i + 1][4] ) { 
				hold = array[i][4]; 
				array[i][4] = array[i + 1][4]; 
				array[i + 1][4] = hold; 
			} 
		} 
    } 
 
	for (i = 0; i < 5; i++) { 	 
		printf("%d ",array[i][4]);
	} 
 
 
    printf("\n\n"); 
 
	//Print the sum of the array 
    printf("The sum of the array is: \n"); 
    int c, r; 
    int sum = 0; 
 
    for (r = 0; r < 4; r++) { 
        for (c = 0; c < 4; c++) { 
            sum = sum + array[r][c]; 
        } 
    } 
    printf("%d", sum); 
}


This post has been edited by Martyn.Rae: 16 March 2010 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#3 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Re: Sorting in descending order

Posted 16 March 2010 - 01:20 PM

Martyn.Rae Firstly i would want to thank you for your help! to say my code was disgraceful is an understatement BUT i must so i am SOOOOOOOOO proud of myself because i am NOT a programing kind of person and i think i did fairly well.
I did look at my mistakes and i want to say thanks again.
The program worked fine but i am still having 2 small (i think they are small) problems.
for the inverse it started for 21
16
11
6
0
i dont print 1, y do you think it that?

the next problem is that i dont think the sum is right. It is giving me 180 for the sum but infact the sum is (I think) 325 (or there about).
Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Sorting in descending order

Posted 16 March 2010 - 01:30 PM

Yes , the inverse code should be

    //Reverse the first column 
    //Print the array on the screen 
 
    printf("The inverse of column 1:\n\n"); 
 
    //get the numbers 
    for (int i = 0; i < 5; i++) { 
        temp[i] = array[i][0]; 
    } 



and for the summation

	//Print the sum of the array 
    printf("The sum of the array is: \n"); 
    int c, r; 
    int sum = 0; 
 
    for (r = 0; r < 5; r++) { 
        for (c = 0; c < 5; c++) { 
            sum = sum + array[r][c]; 
        } 
    } 
    printf("%d", sum); 


Was This Post Helpful? 0
  • +
  • -

#5 Mavado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 14-April 09

Re: Sorting in descending order

Posted 16 March 2010 - 01:53 PM

THANKS ALOT! I REALLY REAALY REAALY AM GRATEFUL FOR ALL YOUR HELP!

My other program is giving me some problems.
this code:
/*Write a program that creates a link list of 10 characters,
then creates a copy of the list in reverse order*/

#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

// self-referential structure
struct linkList_data
{
    char data;   //each listNode contains a character
    struct linkList_data *nextPtr;	//pointer to next node
};	//end structure linkList_data

	char linkList_dataPtr, *sPtr;
	char value;
    char newPtr;      // pointer to new node
    char previousPtr; // pointer to previous node in list
    char currentPtr;  // pointer to current node in list
    linkList_data;
int main ()
{
    newPtr = malloc( sizeof( linkList_data ) ); //create node

    if ( newPtr != NULL )   //is space available
    {
        newPtr -> data = value; // place value in node
        newPtr -> nextPtr = NULL; //node does not link to another node

        previousPtr = NULL;
        currentPtr = *sPtr;

        // loop to find the correct location in the list
        while ( currentPtr != NULL && value > currentPtr->data )
        {
            previousPtr = currentPtr;          /* walk to ...   */
            currentPtr = currentPtr->nextPtr;  /* ... next node */
        } // end while

        // insert new node at beginning of list
        if ( previousPtr == NULL )
        {
            newPtr -> nextPtr = *sPtr;
            *sPtr = newPtr;
        } //end if
        else   //insert new node between previousPtr and currentPtr
        {
            previousPtr -> nextPtr = newPtr;
            newPtr -> nextPtr = currentPtr;
        } //end else

    } //end if
    else
    {
        printf( "%c not inserted. No memory available.\n", value );
    } // end else

} //end function insert

 getch ();
 return 0;


should accept 10 char into a linklist and then copy the list in reverse im getting 2 errors "cannot covert 'void *' to 'char'" AND "pointer to structure required on left side of -> or ->*"
the last error message im getting it about 5 times.

This post has been edited by Mavado: 16 March 2010 - 06:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1