-
No CLUE what to do...
Basically, I have an assignment and have NO clue. Can someone please help? Here is the assignment along with the link for the bubble sort:
In this assignment you are to implement an array of 10 structs, each of which has two members:
Name : which contains strings of arbitrary lengths.
ID : an integer
You should read them in from standard input. The format is one line of name , then another line of id. It continues until the end of file.
Then you are to use bubble-sort to sort the array according to the ID. The output should be id followed by the name, one line per pair. So in total there should be 10 lines.
Bubble Sort
If anyone can help, I would greatly appreciate it. Thank you.
-Frustrated with Structure
-
Hello stweade, I might be able to help.
First question I have is, do you have what the file's layout of data to be inputted looks like?
Example:
Is it like that, or something different?
- Stack Overflow
-
it said read from standard input, which is quite easy heres help with the struct declaration:
Code:
struct info_struct {
short int id;
char *name;
} members[10];
Now see that name will have to be allocated for each name you read in, I assume that is what is required as it said names of arbitrary length.
-
The input will come from the keyboard. The output will look something like this:
Please enter name: Stewart
Please enter ID: 123
Please enter name: Joe
Please enter ID: 456
for 10 names/ids. I hope that answers what you were asking.
-
Code:
#include <stdio.h>
struct info_struct {
int id;
char *name;
} members[10];
int main(void)
{
char buf[BUFSIZ];
int i=0;
while (i<10) {
/* get input here and store it in "buf"
next malloc the sizeof buf for members[i].name, then strcpy buf into members[i].name
next get the id directly into the structure, something like:
fgets(buf, sizeof buf,stdin);
members[i].id = atoi(buf);
of course you'll need error checking for the malloc and the input and all that... */
i++;
}
/* sort and output array of structs here ... */
return 0;
}
If you give us some actual attempt at doing this, we may be able to help, otherwise, we're not just gonna do your homework for you, sorry.
-
This is what I have had so far. I don't know how to read in an integer for the ID and then I don't know how to pass it to the bubble array.
Code:
#include <stdio.h>
void bubbleSort(int numbers[], int array_size);
struct member {
char name[30];
int id[10];
};
int main(void)
{
struct member n[10], d[10];
int i, j, k, *p1, size;
for (i=0; i<10; i++)
{
printf("Please enter name: ");
gets(n[i].name);
printf("Please enter ID: ");
gets(d[i].id);
}
for (k=0; k<10; k++)
{
p1 = d[k].id;
size = 10;
bubbleSort(*p1, size);
}
for (j=0; j<10; j++)
{
printf("%s, %s\n", d[j].id, n[j].name);
}
return 0;
}
void bubbleSort(int &numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
I have attempted this, it's not just a plea to do my homework for me. I really just am having a hard time understanding structure I guess. I dunno...
-
Code:
void bubbleSort(int &numbers[]
Well that's incorrect, for starters, because there are no "references" in C like there are in C++. That is to say, you can't use the reference operator that C++ has.
Consider reading this link on how to read a line correctly from the user, and this link on how to read a number.
Quzah.
-
Wahoo! I changed it to a regular "int" and it worked. Thanks man. I have another part to the problem I have to figure out, so I am going to try and figure that out...you never know, I could be back with more problemos. Thanks again.