- #1

- 54

- 0

## Homework Statement

I have to create a program that will sort an array of structs with random numbers from smallest to largest.

## Homework Equations

This is my code right now.

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

typedef struct

{

int a;

int b;

} pie;

void print_array(pie array[],int size);

void largest_a(pie array[],int size);

int main()

{

pie array[20];

int size = sizeof(array)/sizeof(array[0]);

printf("size of array is %d\n",size);

srand ( time(NULL) );

print_array(array,size);

largest_a(array,size);

return 0;

}

void print_array(pie array[],int size)

{

int i;

for(i = 0; i < size; i++)

{

array

*.a = rand() % 100 + 1;*

array

array

*.b = rand() % 100 + 1;*

}

for(i = 0;i < size; i++)

{

printf("array[%d].a = %d\n",i,array}

for(i = 0;i < size; i++)

{

printf("array[%d].a = %d\n",i,array

*.a);*

printf("array[%d].b = %d\n",i,arrayprintf("array[%d].b = %d\n",i,array

*.b);*

}

}

void largest_a(pie array[],int size)

{

int min = 0;

int i;

int j;

for(i = size-1; i>=0;i--)

{

for(j = 0;j <= i;j++)

{

if(array[j].a < array[j+1].a)

{

min = array[j].a;

array[j].a = array[j+1].a;

array[j+1].a = min;

}

}

printf("Array value %d\n",min);

}

}

The result I get is the following.

size of array is 20

array[0].a = 19

array[0].b = 77

array[1].a = 16

array[1].b = 85

array[2].a = 25

array[2].b = 96

array[3].a = 8

array[3].b = 78

array[4].a = 2

array[4].b = 93

array[5].a = 50

array[5].b = 98

array[6].a = 59

array[6].b = 25

array[7].a = 53

array[7].b = 9

array[8].a = 31

array[8].b = 67

array[9].a = 89

array[9].b = 81

array[10].a = 100

array[10].b = 74

array[11].a = 90

array[11].b = 92

array[12].a = 18

array[12].b = 16

array[13].a = 29

array[13].b = 81

array[14].a = 20

array[14].b = 99

array[15].a = 76

array[15].b = 90

array[16].a = 28

array[16].b = 43

array[17].a = 26

array[17].b = 52

array[18].a = 38

array[18].b = 33

array[19].a = 29

array[19].b = 91

Array value 2

Array value 8

Array value 16

Array value 18

Array value 19

Array value 20

Array value 25

Array value 26

Array value 28

Array value 29

Array value 31

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

I'm trying to figure out why it stops sorting after a certain point.

Any ideas?

Thanks.}

}

void largest_a(pie array[],int size)

{

int min = 0;

int i;

int j;

for(i = size-1; i>=0;i--)

{

for(j = 0;j <= i;j++)

{

if(array[j].a < array[j+1].a)

{

min = array[j].a;

array[j].a = array[j+1].a;

array[j+1].a = min;

}

}

printf("Array value %d\n",min);

}

}

## The Attempt at a Solution

The result I get is the following.

size of array is 20

array[0].a = 19

array[0].b = 77

array[1].a = 16

array[1].b = 85

array[2].a = 25

array[2].b = 96

array[3].a = 8

array[3].b = 78

array[4].a = 2

array[4].b = 93

array[5].a = 50

array[5].b = 98

array[6].a = 59

array[6].b = 25

array[7].a = 53

array[7].b = 9

array[8].a = 31

array[8].b = 67

array[9].a = 89

array[9].b = 81

array[10].a = 100

array[10].b = 74

array[11].a = 90

array[11].b = 92

array[12].a = 18

array[12].b = 16

array[13].a = 29

array[13].b = 81

array[14].a = 20

array[14].b = 99

array[15].a = 76

array[15].b = 90

array[16].a = 28

array[16].b = 43

array[17].a = 26

array[17].b = 52

array[18].a = 38

array[18].b = 33

array[19].a = 29

array[19].b = 91

Array value 2

Array value 8

Array value 16

Array value 18

Array value 19

Array value 20

Array value 25

Array value 26

Array value 28

Array value 29

Array value 31

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

Array value 59

I'm trying to figure out why it stops sorting after a certain point.

Any ideas?

Thanks.