How Stuff Works - C - Sorting a Struct Array

I have to sort an array of structs called "records" in one integer field (does this just mean I have to sort the array based in order?). Here are the specs for the problem:
Create an array of records and write some code to sort that array on one integer field.


Here's my code:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>

struct records
{
	int rec1;
	int rec2;
	int rec3;
	int rec4;
	int rec[4];
};

void sort_records(struct records rec_array[], const int size);
void print(const struct records rec_array[], const int size);

int main()
{
	const int array_size = 4;
	struct records rec_array[4];
	for (int i = 0; i < array_size; ++i)
	{
		rec_array[i].rec[i] = i;
	}
	sort_records(rec_array, array_size);
	return 0;
}

void sort_records(struct records rec_array[], const int size)
{
	int swapped = 0;
	for (int i = 0; i < size; ++i)
	{
		if (rec_array[i].rec[i] > rec_array[i + 1].rec[i + 1])
		{
			int temp = rec_array[i].rec[i];
			rec_array[i].rec[i] = rec_array[i + 1].rec[i + 1];
			rec_array[i + 1].rec[i + 1] = temp;
			swapped = 1;
		}
	}
}

void print(const struct records rec_array[], const int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("%d\n", rec_array[i].rec[i]);
	}
}


For some reason, the program's output is completely empty. I only see the note to press a key to close the window when I run it in Visual Studio. What am I doing wrong that's making the array not be displayed?
1
2
3
4
5
6
7
8
int main()
{
	const int array_size = 4;
	struct records rec_array[4];
	for (int i = 0; i < array_size; ++i) { 	rec_array[i].rec[i] = i; }
	sort_records(rec_array, array_size);
	return 0;
}

Do you see "print" anywhere in that? I don't.

Your program has awfully many 4 in it, even though it does have array_size too. Furthermore, you have coincidences that make difficult to separate logically distinct things.

One record of yours has actually 8 integers. You do set the value of only one of them. You do print the value of only one of them.


In a sort one usually uses two suboperations:

bool comp( const record & lhs, const record & rhs );
Returns true, if lhs is before rhs, i.e. one does not need to change the order of lhs and rhs. All you have to do, is to implement this comp() so that it compares one menber, say 'rec3'.

void swap( record & lhs, record & rhs );
This will exchange the values of lhs and rhs.
The standard library does have std::swap() that does work for record.

Write the sorting routine using the comp() and the std::swap().


Initialize all members of each record.
Show all members of each record.

Show the entire array both before and after sorting so that you can see what the sorting did do.
It's not C++, or I'd have used std::swap(). Didn't I mention that it's C? But yeah, I did forget to call the print() function in main(). My bad there. Does C have a swap() function?
The C does not have templates, function overloading, nor references. A generic function in the library is thus not likely, is it? But you can easily write a
void swap( struct records * lhs, struct records * rhs);
It's jut three lines of code. I did it myself within the same function.

And it seems the main problem was syntax errors. I use the typedef keyword when defining the struct and used "records" without its full name, and was able to do the rest fine as well with the right syntax.

I can just mark this as "solved" now. But yeah, thanks for trying to help.
Topic archived. No new replies allowed.