1.Defined a structure called that has the following field: a: Name that is strin
ID: 639796 • Letter: 1
Question
1.Defined a structure called that has the following field:
a: Name that is string;
b: Gpa that is double;
c:Age that is int;
2.In main create an array of student called student of size 4. Enter values to this array and output the values and check if it works.
3.If part 2 works, define a function to read the values into this array and then test the function.
4.Define a student pointer and place an address of a student variable in it. Place values into that location using the pointer. This is a practice for how to use >operator.
This is my code, but I don't know how to do the part4.
#include <iostream>
using namespace std;
int SIZE;
struct Student
{
string name;
double gpa;
int age;
};
void readValues(Student s[]);
int main()
{
Student students[4];
SIZE= 4;
for(int i = 0; i < 4; i++)
{
cout << "Student:" << i+1 << endl;
cout << "input name:";
cin >> students[i].name;
cout << "input gpa:";
cin >> students[i].gpa;
cout << "input age:";
cin >> students[i].age;
cout << endl;
}
cout << endl;
cout << endl;
readValues(students);
}
void readValues(Student s[])
{
for(int i = 0; i < 4; i++)
{
cout << "Student " << s[i].name << endl;
cout << "age:" << s[i].age << endl;
cout << "gpa:" << s[i].gpa << endl;
}
}
Explanation / Answer
#include <iostream>
using namespace std;
int SIZE;
struct Student
{
string name;
double gpa;
int age;
};
void readValues(Student s[]);
int main()
{
Student students[4];
SIZE= 4;
for(int i = 0; i < 4; i++)
{
cout << "Student:" << i+1 << endl;
cout << "input name:";
cin >> students[i].name;
cout << "input gpa:";
cin >> students[i].gpa;
cout << "input age:";
cin >> students[i].age;
cout << endl;
}
cout << endl;
cout << endl;
readValues(students);
Student *studentPtr = &students[0];
//changing the fields of student[0] through pointer to the Student Structure
studentPtr->name = "Ronaldo";
studentPtr->gpa = 9.7;
studentPtr->age = 30;
// printing new values of struct array using readValues function
cout << endl;
cout << "Printing updated values" << endl;
readValues(students);
}
void readValues(Student s[])
{
for(int i = 0; i < 4; i++)
{
cout << "Student " << s[i].name << endl;
cout << "age:" << s[i].age << endl;
cout << "gpa:" << s[i].gpa << endl << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.