Experiment 08

AIM: Programming using structure and pointer

Task 1: Define structure Student having data members: Roll no, name, address, branch and percentage. WAP to read and display information of a student.

#include <iostream>
using namespace std;
#include <cstring>

struct student
{
int roll;
char name[20];
char add[20];
char branch[20];
float per;
};

int main()
{
struct student s;
cout << "Enter the following information of the student Name, Address, Branch, Rollno, Percent:" << endl;
gets(s.name);
gets(s.add);
gets(s.branch);
cin >> s.roll;
cin >> s.per;
cout << "Rollno: " << s.roll << endl;
cout << "Name: " << s.name << endl;
cout << "Address: " << s.add << endl;
cout << "Branch: " << s.branch << endl;
cout << "Percentage: " << s.per;
return 0;
}

Task 2: Define structure called cricket that will describe the following data Player name, country name, no of matches played & batting avg. Develop a program that store information of 10 players and display names of players having batting average greater than 50.

#include <iostream>
using namespace std;
#include <cstring>

struct player
{
char name[20];
char country[20];
int match;
float avg;
};

int main()
{
int i;
struct player p[2];
for (i = 0; i < 2; i++)
{
cout << "Enter the Name, Country, no. of Matches played and Batting average of the player" << endl;
gets(p[i].name);
gets(p[i].country);
cin >> p[i].match;
cin >> p[i].avg;
}
for (i = 0; i < 2; i++)
if (p[i].avg > 50)
cout << p[i].name << endl;
return 0;
}

Task 3: There are 50 computers in an office. Every computer has following information CPU type, hard disk size. WAP to store details of all 50 computers and then print details of computers having hard disk size greater than 8 GB.

#include <iostream>
using namespace std;
struct cpu
{
char type;
int siz;
};

int main()
{
int i;
struct cpu c[3];
for(i=0; i<3; i++)
{
cout << "Enter the type & size:" << endl;
cin >> c[i].type;
cin >> c[i].siz;
}
for(i=0; i<3; i++)
{
if(c[i].siz > 8)
{
cout << "Type:" << c[i].type << " ";
cout << "Size:" << c[i].siz << endl;
}
}
return 0;
}

Task 4: WAP to print array using pointer

#include <iostream>
using namespace std;
int main()
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = arr;

for (int i = 0; i < 10; ptr++, i++)
cout << *ptr << endl;
return 0;
}

Task 5: WAP to print string in reverse order using pointers

#include <iostream>
using namespace std;
#include <cstring>
int main()
{
int n;
char a[] = {"MPSTME"};
char *ptr;
ptr = a;
n = strlen(a);
ptr += n;
for (int i = n; i >= 0; ptr--, i--)
cout << *ptr << endl;
return 0;
}

Task 6: WAP to copy one string to another using pointer and display copied string-using pointer.

#include <iostream>
using namespace std;
int main()
{
char a[10];
char b[10] = {"MPSTME"};
char *ptr;
ptr = b;
for (int i = 0; i < 10; i++, ptr++)
a[i] = *ptr;
cout << "Copied string: " << a;
return 0;
}

Task 7: WAP to find the number of vowels in entered string using pointer
[eg - i/p India o/p : A-1, E-0, I-2, O-0, U-0]

#include <iostream>
using namespace std;
int main()
{
int a = 0, e = 0, i = 0, o = 0, u = 0;
char A[20];
char *ptr;
ptr = A;
cout << "Enter the string: ";
cin >> A;
for (; *ptr != '\0'; ptr++)
{
switch (*ptr)
{
case 'A':
case 'a':
a++;
break;
case 'E':
case 'e':
e++;
break;
case 'I':
case 'i':
i++;
break;
case 'O':
case 'o':
o++;
break;
case 'U':
case 'u':
u++;
break;
default:
break;
}
}
cout << "A:" << a << " E:" << e << " I:" << i << " O:" << o << " U:" << u;
return 0;
}