Hi, i'm trying to sort a structure but its not set to any specific amount. However i'm trying to apply a bubble sort but the struct is not have an array! Is there any way to sort without an array??
Printable View
Hi, i'm trying to sort a structure but its not set to any specific amount. However i'm trying to apply a bubble sort but the struct is not have an array! Is there any way to sort without an array??
What do you you mean "without an array"? Is it filled with a bunch of different variables?
yes its a struct! but for input and output i used no arrays
Code:struct database{
char fname[50];
char lname[50];
int age;
char gender[50];
int group;
int position;
};
When you have a struct, you sort based on a key of it. For example age. But what exactly you want to do? Sort an array of structs?
Given a single instance of your struct database, what would you expect "sorting" it to achieve?
Ok so i basically want to sort by, as you said, age, but im not usings any arrays throughout my program! so i cant use arrays to sort!
Code:#include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50]; #include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50];
char lname[50];
int age[];
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}#include <stdio.h>
#include <stdlib.h>
struct database{
char fname[50];
char lname[50];
int age[];
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}
char lname[50];
int age;
char gender[50];
int group;
int position;
};//structure named database
void menu(struct database);
void input(struct database);
void files(struct database);
void output(struct database);
void sort(struct database);
void sort_age(struct database);
int count;
int main()
{
struct database ind;//ind_Individual
menu(ind);
}
void menu(struct database ind)
{
int opt;
do{
printf("1. Add\n2. View\n3. Sort\n4. Exit\n\n");
printf("Option: ");
scanf("%d", &opt);
switch(opt)
{
case 1:
input(ind);
break;
case 2:
output(ind);
break;
case 3:
sort(ind);
break;
case 4:
exit(0);
break;
default:
printf("INVALID\n\n");
break;
}
}while(opt!=4);
}
void input(struct database ind)
{
printf("Enter first name: ");
scanf("%s", ind.fname);
printf("Enter last name: ");
scanf("%s", ind.lname);
printf("Enter age: ");
scanf("%d", &ind.age);
printf("Enter gender: ");
scanf("%s", ind.gender);
printf("Enter group[1-4]: ");
scanf("%d", &ind.group);
printf("Enter position[1-3]: ");
scanf("%d", &ind.position);
files(ind);
count++;
}
void files(struct database ind)
{
FILE*fp=fopen("REG.txt", "a");
fprintf(fp,"%s\n", ind.fname);
fprintf(fp,"%s\n", ind.lname);
fprintf(fp,"%d\n", ind.age);
fprintf(fp,"%s\n", ind.gender);
fprintf(fp,"%d\n", ind.group);
fprintf(fp,"%d\n", ind.position);
fclose(fp);
}
void output(struct database ind)
{
int inc;//increment
FILE*fp=fopen("REG.txt", "r");
for(inc=0; inc<count; inc++)
{
fscanf(fp,"%s", ind.fname);
fscanf(fp, "%s", ind.lname);
printf("NAME: %s %s\n", ind.fname, ind.lname);
fscanf(fp,"%d", &ind.age);
printf("AGE: %d\n", ind.age);
fscanf(fp,"%s", ind.gender);
printf("GENDER: %s\n", ind.gender);
fscanf(fp,"%d", &ind.group);
printf("GROUP: %d\n", ind.group);
fscanf(fp,"%d", &ind.position);
printf("POSITION: %d\n\n", ind.position);
}
fclose(fp);
}
void sort(struct database ind)
{
int choice;
do{
printf("How would you like to sort the data?\n\n");
printf("1. Age\n2. Group\n3. Position\n4. Main Menu\n\n");
printf("Choice: ");
scanf("%d", & choice);
switch(choice)
{
case 1:
sort_age(ind);
break;
case 2:
printf("By Group");
break;
case 3:
printf("By Position\n");
break;
case 4:
menu(ind);
break;
default:
printf("INVALID OPTION\n\n");
break;
}
}while( choice != 4);
}
void sort_age(struct database ind)
{
int i, j, temp;
for (i=0; i<= count-2; i++)
{
for (j=0; j<= (count-2) - i; j++)
{
if (ind.age[j] > ind.age[j + 1] )
{
temp = ind.age[j];
ind.age[j] = ind.age[j + 1];
ind.age[j + 1] = temp;
}
}
}
printf ("\n\nData Sorted by Age:\n");
for (i=0; i<= count-1; i++)
printf("%d\t", ind.age[i]);
getch();
}
the sort_age function can't work!
I have an array of ints like this
after sorting I want to achieve this resultCode:int array[5] = {9,6,4,3,2};
In your case what do you want to give for sorting and what to get as result?Code:int array[5] = {2, 3, 4, 6, 9};
i understand sorting...in my case i dont have an array but im reading the entire file!
SO, you want to have many structs inside a file, right? A collection of "things" that are of the same type... If you don't have an array, you can write the 1st struct in the file and then compare the second with the first one and write it before or after the 1st one and so on...
But why not to use an array?
Yes. Use an array. You can post back if you are getting errors ;)
i tried it. the thing is...i dont want to set a definite amount of locations in the array...and without that my program crashes!
From the code of your sort_age() function, it appears you somehow want to have a single struct that contains an array of ages of unspecified size, and you want to sort that array.
Your problem description is fatally flawed. Firstly, it is not possible for a struct to contain an array with unspecified size (except in one special case, but that is not relevant to your problem). Second, if you did have an array of ages in the struct, sorting that array of ages would only have the effect of jumbling up your records (i.e. changing the recorded age of all people listed in the database).
You're losing track of the fact that, with a collection of records of people's data, sorting it should not change the age of anyone - the association of the age with other fields needs to be maintained. You don't want a situation where Joe Bloggs has an age of 42 but, after sorting your database, he is recorded as having an age of 16.
So you need an array of structs, and then sort the array. Not sorting the struct.
You mean that you do not want to have a fixed size for your array? There are some ways to bypass this issue. What I suggest is to use a simple linked list of structs. Here you can find one that handles integers.
But if you just do your hw, it would be fine to use a fixed logical size I guess..
i know...but its destroying the fundamentals of the program! the basic input and output isnt working now!