Pop Module 5 Answer
Pop Module 5 Answer
ANSWERS
#include<stdio.h>
struct employee
{
char name[20];
float salary;
int empno;
};
void main()
{
struct employee e[10];
int n,i;
printf("Enter no.of employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter details of employee %d \n",i+1);
printf("Enter name, salary and employee_number\n");
scanf("%s%f%d",e[i].name,&e[i].salary,&e[i].empno);
}
for(i=0;i<n;i++)
{
printf("\nDetails of employee %d \n",i+1);
printf("Entered name salary and employee_number is:-
\n");
printf("%s\t%f\t%d",e[i].name, e[i].salary,e[i].empno);
}
}
Structure Definition:
Syntax:
struct tagName
{
datatype member1;
datatype member2;
------------
------------
datatype membern;
};
Example :
struct employee
{
char name[20];
float salary;
int empno;
};
struct Rectangle {
int length;
int width;
};
int main() {
struct Rectangle rect = {10, 5};
displayArea(rect.length, rect.width); // Passing individual
members
return 0;
}
• Modes :
• Files in C can be opened in different modes using the fopen()
function, which specifies how the file will be accessed.
Text Modes:
Examples:
1. Write Mode ("w"):
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, World!");
fclose(file);
#include <stdio.h>
int main() {
FILE *sourceFile, *targetFile;
char sourceFileName[100], targetFileName[100];
char ch;
printf("Enter the source file name: ");
scanf("%s", sourceFileName);
printf("Enter the target file name: ");
scanf("%s", targetFileName);
sourceFile = fopen(sourceFileName, "r");
if (sourceFile == NULL) {
printf("Error: Source file does not exist or cannot be
opened.\n");
return 1;
}
targetFile = fopen(targetFileName, "w");
if (targetFile == NULL) {
printf("Error: Target file cannot be opened or created.\n");
fclose(sourceFile);
return 1;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, targetFile);
}
printf("File copied successfully.\n");
fclose(sourceFile);
fclose(targetFile);
return 0;
}
Sample Input/Output:
Input:
Enter the source file name: source.txt
Enter the target file name: target.txt
Output:
File copied successfully.
1. Explain structure declaration and how structure
member are accessed with example?
Ans:
Declaration of a Structure :-
A structure is declared using the keyword struct followed by
structure name and variables are declared within a structure
The structure is usually declared before the main( ) function.
Syntax : -
struct structure_name
{
datatype member 1;
datatype member 2;
datatype member 3;
…………………..
…………………..
datatype member n;
};
(OR)
#include<stdio.h>
struct employee
{
char name[20];
float salary;
int empno;
};
void main()
{
struct employee e;
printf("Enter name, number and salary of employee\n");
scanf("%s%d%f", e.name, &e.empno, &e.salary);
//reading details
printf("Entered name number and salary of employee is\n");
printf("%s \t%d \t%f", e.name, e.empno, e.salary); //
printing details
}
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
int n, i;
float total = 0, average;
return 0;
}
Sample Input/Output:
INPUT:
Enter the number of students: 3
Enter name of student 1: Alice
Enter marks of Alice: 85
Enter name of student 2: Bob
Enter marks of Bob: 70
Enter name of student 3: Charlie
Enter marks of Charlie: 90
OUTPUT:
Average Marks: 81.67
Example Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
printf("Enter source file path: ");
scanf("%s", path);
file = fopen(path, "r");
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
characters = words = lines = 0;
Syntax :-
ptr_data_type *ptr_var_name;
ptr_var_name = &var_name;
where var_name is a variable whose address is to be stored in the
pointer.
Example :-
int a=10;
int *ptr;
then
ptr = &a;
*ptr = a;
Here ptr is a pointer holding the address of variable ‘a’ and *ptr
holds the value of the variable a.
#include<stdio.h>
void main()
{
int *ptr;
int x=22;
printf("Addres of x= %d\n",&x);
printf("Content of x= %d\n",x);
ptr=&x; printf("Addres of pointer= %d\n",ptr);
printf("Content of pointer= %d\n",*ptr);
}
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
- SACHIN
- MOHAMMED MAZZ
(E – section ACET)