Structures and pointers
Task 1:
Write a program to create structure named employee. Take information of employee from user as input (EmpID, EmpName, EmpAge, EmpSalary) Display the output.
#include<iostream>
#include<string>
using namespace std;
struct employ{
int id;
string name;
int age;
int salary;
};
int main()
{
employ inform;
cout << "Enter the id of the employ:";
cin >> inform.id;
cin.ignore();
cout << "Enter the name of the employ";
getline(cin, inform.name);
cout << "Enter the age of the employ:";
cin >> inform.age;
cout << "Enter the salary of the employ:";
cin >> inform.salary;
cout << "The id of the employ is:" << inform.id << endl;
cout << "The name of the employ is:" << inform.name << endl;
cout << "The age of the employ is:" << inform.age << endl;
cout << "The salary of the employ is:" << inform.salary << endl;
getchar();
getchar();
return 0;
}
Task 2:
Perform Task 1 using pointer structure.
#include<iostream>
#include<string>
using namespace std;
struct employ{
int id;
string name;
int age;
int salary;
};
void out(struct employ);
void out_ref(struct employ*);
int main()
{
employ inform;
cout << "Enter the id of the employ:";
cin >> inform.id;
cin.ignore();
cout << "Enter the name of the employ:";
getline(cin, inform.name);
cout << "Enter the age of the employ:";
cin >> inform.age;
cout << "Enter the salary of the employ:";
cin >> inform.salary;
out(inform);
out_ref(&inform);
getchar();
getchar();
return 0;
}
void out(struct employ inform)
{
cout << "The id of the employ is:" << inform.id << endl;
cout << "The name of the employ is:" << inform.name << endl;
cout << "The age of the employ is:" << inform.age << endl;
cout << "The salary of the employ is:" << inform.salary << endl;
}
void out_ref(struct employ *inform)
{
cout << "The id of the employ is:" << inform->id << endl;
cout << "The name of the employ is:" << inform->name << endl;
cout << "The age of the employ is:" << (*inform).age << endl;
cout << "The salary of the employ is:" << inform->salary << endl;
}
Task 3:
Perform Task 1 and display the output using function. Pass the structure object in function first by value and then by reference.
#include<iostream>
#include<string>
using namespace std;
struct employ{
int id;
string name;
int age;
int salary;
};
void out(struct employ);
void out_ref(struct employ&);
int main()
{
employ inform;
cout << "Enter the id of the employ:";
cin >> inform.id;
cin.ignore();
cout << "Enter the name of the employ:";
getline(cin, inform.name);
cout << "Enter the age of the employ:";
cin >> inform.age;
cout << "Enter the salary of the employ:";
cin >> inform.salary;
out(inform);
out_ref(inform);
getchar();
getchar();
return 0;
}
void out(struct employ inform)
{
cout << "The id of the employ is:" << inform.id << endl;
cout << "The name of the employ is:" << inform.name << endl;
cout << "The age of the employ is:" << inform.age << endl;
cout << "The salary of the employ is:" << inform.salary << endl;
}
void out_ref(struct employ &inform)
{
cout << "The id of the employ is:" << inform.id << endl;
cout << "The name of the employ is:" << inform.name << endl;
cout << "The age of the employ is:" << inform.age << endl;
cout << "The salary of the employ is:" << inform.salary << endl;
}
Task 4:
Enter the marks of 5 students in Computer Programming, ICT and Object-Oriented Programming (each out of 100) using a structure named Marks having elements roll no., name, cp_marks, ict_marks and oop_marks and then display the percentage of each student.
#include<iostream>
#include<string>
using namespace std;
struct marks{
int roll;
string name;
int cp_marks;
int ict_marks;
int oop_marks;
};
int main()
{
marks no[5];
int per[5];
for (int i = 0; i < 2; i++)
{
cout << "Enter the roll number of the student:";
cin >> no[i].roll;
cin.ignore();
cout << "Enter the name of the student:";
getline(cin, no[i].name);
cout << "Enter the marks of cp:";
cin >> no[i].cp_marks;
cout << "Enter the marks of ict:";
cin >> no[i].ict_marks;
cout << "Enter the marks of opp:";
cin >> no[i].oop_marks;
per[i] = (no[i].cp_marks + no[i].ict_marks + no[i].oop_marks) / 3;
cout << "Percentege of student " << i + 1 << ":" << per[i] << endl;
}
getchar();
getchar();
return 0;
}
Task 5:
Write a structure to store the name, account number and balance of 50 customers and store their information.
1 - Write a function to print the names of all the customers having balance less than $200.
2 - Write a function to add $100 in the balance of all the customers having more than $1000
in their balance and then print the incremented value of their balance.
#include<iostream>
#include<string>
using namespace std;
struct info{
string name;
int acc;
int balence;
};
void low(struct info data[50],int);
void add(struct info[50],int);
int main()
{
info data[50];
int size;
cout << "How many customer has the data:";
cin >> size;
for (int i = 0; i < size; i++)
{
cin.ignore();
cout << "Enter the name of the customer:";
getline(cin, data[i].name);
cout << "Enter the account number of the customer:";
cin >> data[i].acc;
cout << "Enter the balence of the customer:";
cin >> data[i].balence;
}
low(data, size);
add(data,size);
getchar();
getchar();
return 0;
}
void low(struct info data[],int size)
{
for (int i = 0; i < size; i++)
{
if (data[i].balence < 200)
{
cout << "Customer " << i + 1 << " name which has the balence less than 200 is:" << data[i].name << endl;
cout << "Customer " << i + 1 << " balence is:" << data[i].balence << endl;
}
}
}
void add(struct info data[], int size)
{
for (int i = 0; i < size; i++)
{
if (data[i].balence>1000)
{
cout << "Balnce with increment is " << data[i].balence + 100 << " for customer " << i + 1 <<endl;
}
}
}
Comments
Post a Comment