Reputation: 3
I have written a program in C that sorts car records by year & prints them, model & prints them, and the finds and prints the duplicates. However, the only time any records print is in the duplicate function. Otherwise, the outputs are totally blank... I have tried compiling in DevC++ and on Linux. The records are a globally declared struct and I'm printing them using a for loop... help!
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct Car //struct for cars
{
char make[20];
char model[20];
int year;
char color[20];
} usedCars[10] = //user inputted cars
{
{"toyota","matrix",2006,"silver"},
{"honda","accord",2009,"blue"},
{"chrysler","ptcruiser",2001,"red"},
{"volvo","xc70",2010,"blue"},
{"chevy","blazer",2001,"black"},
{"ford","f150",1998,"blue"},
{"jeep","grandcherokee",2008,"red"},
{"cadillac","deville",2004,"red"},
{"volkswagen","jetta",2010,"silver"},
{"chrysler","ptcruiser",2001,"red"}
};
void print_cars_by_year() //prints by year
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (usedCars[j].year > usedCars[j+1].year) //if the years of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by year are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints newly sorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_cars_by_model()
{
char temp[20]; //array for swapping records
int i,j,tmp; //variables for swapping
for (i=0; i<10; i++) // nested loop for bubble sort - repeats for 10 records
{
for (j=0 ; j<9; j++) //bubble sort loop
{
if (strcmp(usedCars[j].model, usedCars[j+1].model)>0) //if the models of the first two records are not equal
{
strcpy(temp, usedCars[j].make); //uses strcpy to swap the records of make
strcpy(usedCars[j].make, usedCars[j+1].make);
strcpy(usedCars[j+1].make, temp);
strcpy(temp, usedCars[j].model); //uses strcpy to swap model
strcpy(usedCars[j].model, usedCars[j+1].model);
strcpy(usedCars[j+1].model, temp);
tmp = usedCars[j].year; //typical int bubble sort
usedCars[j].year = usedCars[j+1].year;
usedCars[j+1].year = tmp;
strcpy(temp, usedCars[j].color); //uses strcpy to swap color
strcpy(usedCars[j].color, usedCars[j+1].color);
strcpy(usedCars[j+1].color, temp);
}
}
}
printf("\nThe records sorted by model are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
void print_duplicate_records()
{
int i,j;
for (i=0; i<10; i++)
{
for (j=i+1; j<10; j++) //will not compare same record
{
if (strcmp(usedCars[i].model, usedCars[j].model) == 0) //only go if models are equal
{
if (strcmp(usedCars[i].make, usedCars[j].make) == 0) //only go if makes are equal
{
if (usedCars[i].year==usedCars[j].year) //only go if years are equal
{
if (strcmp(usedCars[i].color, usedCars[j].color) == 0) //if colors are equal, the record is equal
{
printf("\nThere is a duplicate record:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
}
}
}
}
}
}
int main()
{
int i;
printf("The records (unsorted) are:\n");
printf("Make\t\tModel\t\tYear\tColor\n");
for (i=0; i<10; i++); //prints unsorted records
{
printf("%s\t%s\t%d\t%s\n",usedCars[i].make,usedCars[i].model,usedCars[i].year,usedCars[i].color);
}
print_cars_by_year(); //call to by year func
print_cars_by_model(); //call to by model func
print_duplicate_records();
return 0;
}
The output looks like
The records (unsorted) are: Make Model Year Color 0
(then the same for the two sort functions)
There is a duplicate record: Make Model Year Color chrysler ptcruiser 2001 red
Upvotes: 0
Views: 76
Reputation: 53006
You have a misplaced semicolon here
for (i=0; i<10; i++);
/* ^ what? */
this means that the code following will only execute once.
Also, please format your code, that will prevent this kind of mistake, also use compiler warnings, I didn't want to read the code because it's too messy then I tried to compile it with warnings and clang
complained about the semicolon, like this
error: for loop has empty body [-Werror,-Wempty-body]
As you see, I found the problem without reading the code, because I used the tools available to check that for me.
The more important thing you should fix, is the many if
statements in your code, you can use the &&
operator to avoid so many levels of indentation.
Upvotes: 4