Issue
All I really need is just examples on how to return these kind of functions to it's caller. Doesn't even have to relate to my code except for the type of function and parameter.
I wrote this function below that is supposed to read the name and age of three user defined people.
void readData(Person *p)
{
char name1[20], name2[20], name3[20];
int age1, age2, age3;
printf("Enter person 1: \n");
scanf("%s %d", &name1, &age1);
printf("Enter person 2: \n");
scanf("%s %d", &name2, &age2);
printf("Enter person 3: \n");
scanf("%s %d", &name3, &age3);
struct person1 {char name1[20]; int age1};
struct person2 {char name2[20]; int age2};
struct person3 {char name3[20]; int age3};
struct Person {struct person1; struct person2; struct person3};
struct Person man[3] = {{name1, age1}, {name2, age2}, {name3, age3}};
p = &man;
}
I would like to re-use the variables of the above function so that I can use it in the function below. How am I supposed to do that as I cannot 're-declare' the variables because they are user-defined? Do I call by reference or value and if so, how do I do that? Do I need to return
anything? I can't find examples for this kind of functions, any help is really appreciated.
Person findMiddleAge(Person *p)
{
int middle;
if ((age1 < age2 && age2 < age3) || (age3 < age2 && age2 < age1)) {
middle = age2;
struct Person man[1] = {name2, age2};
}
else if ((age2 < age1 && age1 < age3) || (age3 < age1 && age1 < age2)) {
middle = age1;
struct Person man[1] = {name1, age1};
}
else if ((age1 < age3 && age3 < age2) || (age2 < age3 && age3 < age1)) {
middle = age3;
struct Person man[1] = {name3, age3};
}
printf("%d \n", middle);
}
The chunk of code below shows my whole code just in case it matters. Nothing can be amended except for the lines of code within the readData(Person *p)
and Person findMiddleAge(Person *p)
functions.
typedef struct {
char name[20];
int age;
} Person;
void readData(Person *p);
Person findMiddleAge(Person *p);
int main()
{
Person man[3], middle;
readData(man);
middle = findMiddleAge(man);
printf("findMiddleAge(): %s %d\n", middle.name, middle.age);
return 0;
}
void readData(Person *p)
{
char name1[20], name2[20], name3[20];
int age1, age2, age3;
printf("Enter person 1: \n");
scanf("%s %d", &name1, &age1);
printf("Enter person 2: \n");
scanf("%s %d", &name2, &age2);
printf("Enter person 3: \n");
scanf("%s %d", &name3, &age3);
struct person1 {char name1[20]; int age1};
struct person2 {char name2[20]; int age2};
struct person3 {char name3[20]; int age3};
struct Person {struct person1; struct person2; struct person3};
struct Person man[3] = {{name1, age1}, {name2, age2}, {name3, age3}};
p = &man;
}
Person findMiddleAge(Person *p)
{
int middle;
if ((age1 < age2 && age2 < age3) || (age3 < age2 && age2 < age1)) {
middle = age2;
struct Person man[1] = {name2, age2};
}
else if ((age2 < age1 && age1 < age3) || (age3 < age1 && age1 < age2)) {
middle = age1;
struct Person man[1] = {name1, age1};
}
else if ((age1 < age3 && age3 < age2) || (age2 < age3 && age3 < age1)) {
middle = age3;
struct Person man[1] = {name3, age3};
}
printf("%d \n", middle);
}
And yes, this is homework but I really tried my best (I tried over 6 hours to get the desired results but I still can't, C is really hard for me to understand). I am supposed to find the person whose age is the middle of the three people, and return the name and age of that person to the caller. Am I going about everything incorrectly? I actually just want to know how to return this kind of functions to the caller. Do I need to put any return
statements? I can't find examples on it (if anyone has any examples, I think I can figure the code out myself with those examples)?
Thank you for any help in advance, I really appreciate it.
Solution
In readData
and findMiddleAge
p
points to man[3]
in main
.
In readData
the array may be filled directly. It is a good habit to check the return of scanf
. If name and age are scanned, it will return 2. A return of 1, 0 or EOF indicates a problem.
In findMiddleAge
determine which is the middle value of p[0].age
, p[1].age
or p[2].age
and return the correct one.
#include <stdio.h>
typedef struct {
char name[20];
int age;
} Person;
void readData(Person *p);
Person findMiddleAge(Person *p);
int main()
{
Person man[3] = { { "", 0}}, middle = { "", 0};
readData(man);
middle = findMiddleAge(man);
printf("findMiddleAge(): %s %d\n", middle.name, middle.age);
return 0;
}
void readData(Person *p)
{
printf("Enter person 1: \n");
scanf("%19s %d", p[0].name, &p[0].age);
printf("Enter person 2: \n");
scanf("%19s %d", p[1].name, &p[1].age);
printf("Enter person 3: \n");
scanf("%19s %d", p[2].name, &p[2].age);
}
Person findMiddleAge(Person *p)
{
//figure out which is the middle value of
//p[0].age
//p[1].age
//p[2].age
return p[1];//or p[0] or p[2]
}
Answered By - user3121023 Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.