- #1
Firestrider
- 104
- 0
Homework Statement
I need to be able to copy full structs in a temporary array based on a value in that struct (an integer), but I'm not sure exactly how to go about it. I have commented where I need to copy over structs.
Also in the other code block, what is the problem here? It doesn't seem to reconize the string.
Homework Equations
N/A
The Attempt at a Solution
Code:
void Merge(struct student_birthday birthdays[], int start, int middle, int end)
{
int* temp, i, length, count1, count2, mc;
length = end - start + 1;
temp = calloc(length, sizeof(struct student_birthday));
count1 = start;
count2 = middle;
mc = 0;
while ((count1 < middle) && (count2 <= end))
{
if (birthdays[count1].ymd < birthdays[count2].ymd)
{
temp[mc] = birthdays[count1].ymd; // structs not values!
count1++;
mc++;
}
else
{
temp[mc] = birthdays[count2].ymd; // structs not values!
count2++;
mc++;
}
}
if (count1 < middle)
{
for (i = mc; i < length; i++)
{
temp[i] = birthdays[count1].ymd; // structs not values!
count1++;
}
}
else if (count2 <= end)
{
for (i = mc; i < length; i++)
{
temp[i] = birthdays[count2].ymd; // structs not values!
count2++;
}
}
for (i = start; i <= end; i++)
birthdays[i].ymd = temp[i - start]; // structs not values!
free(temp);
}
Code:
int MonthToInt(char month[9]);
struct student_birthday
{
char month[9];
};
int main(void)
{
m_int = MonthToInt(birthdays[j].month);
}
int MonthToInt(char month[9])
{
int m_int = 0;
if (month == "JANUARY") m_int = 1;
else if (month == "FEBRUARY") m_int = 2;
else if (month == "MARCH") m_int = 3;
else if (month == "APRIL") m_int = 4;
else if (month == "MAY") m_int = 5;
else if (month == "JUNE") m_int = 6;
else if (month == "JULY") m_int = 7;
else if (month == "AUGUST") m_int = 8;
else if (month == "SEPTEMBER") m_int = 9;
else if (month == "OCTOBER") m_int = 10;
else if (month == "NOVEMBER") m_int = 11;
else if (month == "DECEMBER") m_int = 12;
return m_int;
}