I am working on a program to rewrite an assortment of integers in increasing order yet they are in decreasing order. I am pretty sure the problem is in my indexMax or sortArray functions but I cant find it. Can anyone help?

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

#define size 7

void sortArray (int num[]);
int indexMax(int num[], int low, int high);
void swap (int num[], int loc1, int loc2);
void getArray (int num[]);
void displayArray (int num[]);

int main()
{
	int num[size];
	
	getArray(num);
	sortArray(num);
	displayArray(num);
}

void getArray (int num[])
{
	int i;
	for (i=0; i<size; i++)
	{
		printf("Enter integer: \n");
		num[i]=GetInteger();
	}
}

void displayArray (int num[])
{
	int i;
	printf("\nThe sorted list is: \n");
	for (i=0; i<size; i++)
	{
		printf("%d\n", num[i]);
	}
}

void sortArray (int num[])
{
	int i, maxInd;
	for (i=0; i<size; i++)
	{
		maxInd=indexMax (num, i, size-1);
		swap (num, i, maxInd);
	}
}

int indexMax (int num[], int low, int high)
{
	int i, maxInd;
	maxInd=low;
	for (i=low;i<=high;i++)
	{
		if (num[i]<num[maxInd])
		{
			maxInd=i;
		}
	}
	return (maxInd);
}

void swap (int num[], int loc1, int loc2)
{
	int temp;
	temp=num[loc1];
	num[loc1]=num[loc2];
	num[loc2]=temp;
}

I've rewritten slightlly the code referring to getArray via iostream as I do not have the necessary includes, however I haven't change your sorting algoritm. The array is displaying the values in ascending order.

#include <iostream>


#define size 7

void sortArray (int num[]);
int indexMax(int num[], int low, int high);
void swap (int num[], int loc1, int loc2);
void getArray (int num[]);
void displayArray (int num[]);

int main()
{
	int num[size];

	getArray(num);
	sortArray(num);
	displayArray(num);
}

void getArray (int num[])
{
	int i;
	for (i=0; i<size; i++)
	{
		printf("Enter integer: \n");
		int anInt;
		std::cin >> anInt;
		num[i]=anInt;
	}
}

void displayArray (int num[])
{
	int i;
	printf("\nThe sorted list is: \n");
	for (i=0; i<size; i++)
	{
		printf("%d\n", num[i]);
	}
}

void sortArray (int num[])
{
	int i, maxInd;
	for (i=0; i<size; i++)
	{
		maxInd=indexMax (num, i, size-1);
		swap (num, i, maxInd);
	}
}

int indexMax (int num[], int low, int high)
{
	int i, maxInd;
	maxInd=low;
	for (i=low;i<=high;i++)
	{
		if (num[i]<num[maxInd])
		{
			maxInd=i;
		}
	}
	return (maxInd);
}

void swap (int num[], int loc1, int loc2)
{
	int temp;
	temp=num[loc1];
	num[loc1]=num[loc2];
	num[loc2]=temp;
}

The result is this:

Enter integer:
9
Enter integer:
5
Enter integer:
2
Enter integer:
1
Enter integer:
6
Enter integer:
7
Enter integer:
3

The sorted list is:
1
2
3
5
6
7
9

Process returned 0 (0x0) execution time : 7.324 s
Press any key to continue.

From this standpoint it looks the way you wanted it, or?

Cheers,

poliet

Oh sorry I mixed up "increasing" and "decreasing" I was in a rush. Your output is the same I've been getting. For that input the output should be
9
7
6
5
3
2
1

Make a reverse function, which is using your swap function. Then call it in main before displayArray()

void reverse(int num[])

void reverse(int num[])
    {
    for (int i=0; i<int((size)/2);i++)
        swap (num, i, size-1-i);
    }

int main()
{
    int num[size];

    getArray(num);
    sortArray(num);
    reverse(num);
    displayArray(num);
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Or, if you do not want to have a reserve function. The problem is in your algorithm in your function indexMax. Change the order as written beneath, and you will have an array that will result in an descending order. However, more natural feels rather a sorting algoritm in ascending order in combination with a reverse array function. Hope that helps.

int indexMax (int num[], int low, int high)
{
	int i, maxInd;
	maxInd=low;
	for (i=high;i>=low;i--)
	{
		if (num[i]>num[maxInd])
		{
			maxInd=i;
		}
	}
	return (maxInd);
}
This question has already been answered. Start a new discussion instead.