0

I am working on "tideman" from pset3 and stuck on sorting the data that i have in "struct" data type. Don't really know if it was supposed to be figured out on our own, but they did not cover the syntax or any function to "swap" the elements in the "struct" data type.

Please help me move forward.

UPDATED I wasn't able to define the problem earlier properly. I have written a sample code to define it, as follows:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

typedef struct
{
    string name;
    int age;
}
people;


int main(void)
{


    int x = get_int("No. of people:");
    people person[x];

    for (int i = 0; i < x; i++)
    {
        person[i].name = get_string("%i) Name: ", (i+1));
        person[i].age = get_int("%i) Age: ", (i+1));
    }

    int a = get_int ("a: ");
    int b = get_int ("b: ");

}

PLEASE JUST POINT IT OUT IF THE OBJECTIVE OF THIS pset IS TO LET STUDENTS FIGURE THIS OUT*

Now, if it's okay with the "Academic Honesty", in the above code, lets say the user enters data of 4 people (Name and Age). Then in the last two lines of code, the user enters the "serial number" (as in the location of those people in the "person array") of the people who's entry must be "swapped" or "switched" with one another in the "person struct-array"...

Hope I am able to convey the problem now..

  • No problems with academic honesty here. We're disucssing a basic technique of how to swap structs. We're not actually doing a pset, although I do know exactly where you're going to use it. ;-) – Cliff B Sep 4 at 6:57
1

It's not really clear what you mean by "swapping elements". I can't see why you'd change one element in a struct, but I can see how you might want to swap entire structs.

Say that you have a struct like this:

typedef struct
{
    string name;
    int votes;
}
candidate;

Now, say that you have two structs, a and b, and they have been populated with data. (the same logic would apply if they were pointers)

Here's how you can swap them:

candidate temp = NULL;
temp = a;
a = b;
b = temp;

Its that simple. When working with vars of the same struct definition, you can exchange the entire struct with a simple "=" assignment. C will automatically copy all of the structure elements within the struct. If they were pointers, it's the same logic because it would just be swapping/copying addresses of the struct.

Now, if you're trying to swap a single element from one struct to another, it's the same logic, but you'd be working with the individual elements. You would just have to specify the individual element name along with the struct var name, either using the -> for struct pointers or "." for named vars or array elements.

[EDIT: after question was updated.] It's as I said above. It's a simple swap. Whole structs can be swapped at once. You just need an intermediate struct to hold one of the values while swapping.

people temp;        // used to temporarily hold data while swapping.
temp = person[a];   // copy a to temp
person[a] = person[b];  //move b to a
person[b] = temp;       //move a from temp to b
//swapped!

If you like, you can copy this into your test program and try it out.

You don't need to swap the individual elements. C will copy entire structures. In fact, it's bad practice to move elements of a struct individually. It adds needless code and can lead to errors.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thank you for the reply @Cliff B. However I wan't able to convey the problem properly and hence I have updated the Question now. Please review the problem. – Sayyed Gauss Sep 4 at 6:30
  • Thanks for your help, it worked out correctly :-) – Sayyed Gauss Sep 17 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .