Structure Questions:
Q: Enter address(house no, block, city, state) of 5 people.
#include <stdio.h>
#include <string.h>
typedef struct personDet {
int house;
int block;
char city[50];
char state[50];
} pd;
int main() {
pd p1;
pd p2;
pd p3;
pd p4;
pd p5;
// Person 1 info
p1.house = 101;
p1.block = 11;
strcpy(p1.city, "Gwalior");
strcpy(p1.state, "MP");
// Person 2 info
p2.house = 102;
p2.block = 12;
strcpy(p2.city, "Jhansi");
strcpy(p2.state, "UP");
// Person 3 info
p3.house = 103;
p3.block = 11;
strcpy(p3.city, "Pune");
strcpy(p3.state, "MP");
// Person 4 info
p4.house = 104;
p4.block = 14;
strcpy(p4.city, "Amhadabad");
strcpy(p4.state, "Gujrat");
// Person 5 info
p5.house = 105;
p5.block = 15;
strcpy(p5.city, "Banglore");
strcpy(p5.state, "MP");
printf("Person 1 info\n");
printf("p1.house : %d\n", p1.house);
printf("p1.block : %d\n", p1.block);
printf("p1.city : %s\n", p1.city);
printf("p1.state : %s\n", p1.state);
printf("\n");
printf("Person 2 info\n");
printf("p2.house : %d\n", p2.house);
printf("p2.block : %d\n", p2.block);
printf("p2.city : %s\n", p2.city);
printf("p2.state : %s\n", p2.state);
printf("\n");
printf("Person 3 info\n");
printf("p3.house : %d\n", p3.house);
printf("p3.block : %d\n", p3.block);
printf("p3.city : %s\n", p3.city);
printf("p3.state : %s\n", p3.state);
printf("\n");
printf("Person 4 info\n");
printf("p4.house : %d\n", p4.house);
printf("p4.block : %d\n", p4.block);
printf("p4.city : %s\n", p4.city);
printf("p4.state : %s\n", p4.state);
printf("\n");
printf("Person 5 info\n");
printf("p5.house : %d\n", p5.house);
printf("p5.block : %d\n", p5.block);
printf("p5.city : %s\n", p5.city);
printf("p5.state : %s\n", p5.state);
return 0;
}
OR
#include <stdio.h>
#include <string.h>
typedef struct personDet {
int house;
int block;
char city[50];
char state[50];
} pd;
void printInfo(pd p, int count);
int main() {
pd p1;
pd p2;
pd p3;
pd p4;
pd p5;
// Person 1 info
p1.house = 101;
p1.block = 11;
strcpy(p1.city, "Gwalior");
strcpy(p1.state, "MP");
// Person 2 info
p2.house = 102;
p2.block = 12;
strcpy(p2.city, "Jhansi");
strcpy(p2.state, "UP");
// // Person 3 info
p3.house = 103;
p3.block = 11;
strcpy(p3.city, "Pune");
strcpy(p3.state, "MP");
// // Person 4 info
p4.house = 104;
p4.block = 14;
strcpy(p4.city, "Amhadabad");
strcpy(p4.state, "Gujrat");
// // Person 5 info
p5.house = 105;
p5.block = 15;
strcpy(p5.city, "Banglore");
strcpy(p5.state, "MP");
printInfo(p1, 1);
printInfo(p2, 2);
printInfo(p3, 3);
printInfo(p4, 4);
printInfo(p5, 5);
return 0;
}
void printInfo(pd p, int count) {
printf("Person %d info\n", count);
printf("p%d.house : %d\n", count, p.house);
printf("p%d.block : %d\n", count, p.block);
printf("p%d.city : %s\n", count, p.city);
printf("p%d.state : %s\n", count, p.state);
printf("\n");
}
Q: Create a two-dimensional vector using structures in C.
#include <stdio.h>
struct vector{
int i,j;
};
int main() {
struct vector v1 = {1, 2};
struct vector v2 = {3, 4};
printf("The value of vector 1 is %di + %dj\n", v1.i, v1.j);
printf("The value of vector 2 is %di + %dj\n", v2.i, v2.j);
return 0;
}
Q: Create a structure to store vectors. Then make a function to return sum of 2 vectors.
#include <stdio.h>
struct vector {
int x;
int y;
};
void calcSum(struct vector v1, struct vector v2, struct vector sum);
struct vector sum_vector(struct vector v1, struct vector v2);
int main() {
struct vector v1 = {5,10};
struct vector v2 = {3,5};
struct vector sum = {0};
calcSum(v1, v2, sum);
printf("The sum vector is : %di %dj\n", sum_vector(v1, v2));
return 0;
}
void calcSum(struct vector v1, struct vector v2, struct vector sum) {
sum.x = v1.x + v2.x;
sum.y = v1.y + v2.y;
printf("Sum of vectors : %di + %dj\n", sum.x, sum.y);
}
// OR
struct vector sum_vector(struct vector v1, struct vector v2) {
struct vector v3 = {v1.x + v2.x, v1.y + v2.y};
return v3;
}
Q: Create a structure to store complex numbers.(use arrow operator).
#include <stdio.h>
struct complex {
int real;
int img;
};
int main() {
struct complex num1 = {5,10};
struct complex *ptr = &num1;
printf("Real part : %d\n", ptr->real);
printf("Imaginary part : %d\n", ptr->img);
return 0;
}
Q: Create an array of 5 complex numbers created in above and display them with the help of a display function. The values must be taken as an input from the user.
#include <stdio.h>
typedef struct complex_num{
int real, img;
} com;
void show(com c);
int main() {
com c[5];
int i = 0;
while(i < 5) {
printf("Enter real value: ");
scanf("%d", &c[i].real);
printf("Enter imaginary value: ");
scanf("%d", &c[i].img);
show(c[i]);
printf("\n");
i++;
}
return 0;
}
void show(com c) {
printf("Complex number is %d + %di", c.real, c.img);
}
Q: Twenty integers are to be stored in memory. What will you prefer- Array or
structure?
Q: Write a program to illustrate the use of arrow operator → in C.
#include <stdio.h>
struct student{
char name[20];
int age;
};
int main() {
// Student 1
struct student s1;
struct student* ptr1 = &s1; // Pointer
strcpy(s1.name, "Mohan");
ptr1->age = 20;
// Student 2
struct student s2;
struct student* ptr2 = &s2;
strcpy(s2.name, "Rohan");
ptr2->age = 40;
printf("Student 1 info...\n");
printf("Name is %s\n", ptr1->name);
printf("Age is %d\n", ptr1->age);
printf("Student 2 info...\n");
printf("Name is %s\n", ptr2->name);
printf("Age is %d\n", ptr2->age);
return 0;
}
Q: Make a struncture to store Bank Account information of a customer of ABC Bank. Also, make an alias for it.
#include <stdio.h>
typedef struct bankHolderInfo {
char branch[100];
char holderName[50];
int accNum;
int customerID;
}bank;
int main() {
// Option 1
bank acc1;
strcpy(acc1.branch, "Lashkar");
strcpy(acc1.holderName, "Sanskar");
acc1.accNum = 123;
acc1.customerID = 345;
// Option 2
bank acc2 = {"Pune", "Vivek", 9567, 856776};
printf("Accunt 1 : %s %s %d %d\n", acc1.branch, acc1.holderName, acc1.accNum, acc1.customerID);
printf("Accunt 2 : %s %s %d %d", acc2.branch, acc2.holderName, acc2.accNum, acc2.customerID);
return 0;
}
0 Comments