Professional Documents
Culture Documents
Assignment-10 - Sreya Saha - 27-07-21
Assignment-10 - Sreya Saha - 27-07-21
DEPARTMENT: CSE_ ROLL NO.: 14800120081 SEMESTER: 2ND GROUP: A YEAR: 1ST
OBJECTIVE: READING & WRITING OF A FILE, COUNTING THE NUMBERS OF WORDS, LINES & CHARACTERS IN A
PARAGRAPH, SORTING OF FILE.
CO-WORKERS:
1 ………………………………………..
MARKS/GRADE: …………………………………
1
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
TO PERFORM ARITHMETIC
OPERATION 5-6
TO CONVERT THE
TEMPERATURE 7-8
TO COMPUTE SIMPLE
AND COMPOUND 10-11
INTEREST
2
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
Assignment Date of Title of Page Teacher’s
No./Expt. No. Performance Assignment/Expt. Remarks Signature
No.
TO CHECK WETHER A YEAR IS
LEAP YEAR OR NOT. 5-6
TO CHECK WETHER A
NUMBER IS PRIME OR NOT 7-8
3
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
Assignment Date of Title of Teacher’s
No./Expt. No. Performance Assignment/Expt. Page No. Remarks
Signature
TO FIND REVERSE OF AN
INTEGER NUMBER
23-24
SERIES
05. 22/06 17-18
PATTERNS 22-28
4
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
Assignment Date of Title of Teacher’s
No./Expt. Performance Page No. Remarks
Assignment/Expt. Signature
No.
TO SEARCH AN ELEMENTS IN
AN ARRAY (LINEAR/BINARY
22-25
SEARCH)
CHARACTER COUNT
WITHOUT USING THE SYSTEM 19
FUNCTION.
5
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
TO GENERATE THE
PASCAL’S TRIANGLE 10-12
USING FUNCTIONS.
6
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
TO CALCULATE THE
AREA OF A TRIANGLE,
RECTANGLE OR A 19-23
CIRCLE USING
FUNCTION
TO SWAP TWO
09 20\07 NUMBERS USING 8-9
POINTERS &
FUNCTION
TO SORT AN ARRAY OF
NUMBERS USING 10-11
POINTERS &
FUNCTION (USE
BUBBLE SORT).
TO SORT AN ARRAY OF
STRINGS USING
POINTERS & 14-15
FUNCTION
(DICTIONARY SORTING
7
Future Institute of Engineering & Management
Department of Computer Science & Engineering
CONTENT
Assignment Date of Title of Teacher’s
No./Expt. Performance Page No. Remarks
Assignment/Expt. Signature
No.
SORTING OF FILE.
8
Q.1.
Problem Statement:
Write a Program in C to write a line of text into a file (xyz.txt) and read from that file to print.
Algorithm:
Step1:Start
Step2:declaring the main()
Step3:declaring a file pointer 2 integer variables and 2 character arrays and a character
variable
Step4:storing the lines as input in the file from the user by opening the file in reading
mode
Step5:opening the file in reading mode
Step6:displaying the contents in the file
Step7:end of main()
Step8:end
C Code:
#include <stdio.h>
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20]="test.txt";
char str1;
fflush(stdin);
printf("\n :: The lines are ::\n");
fptr = fopen (fname,"w");
fgets(str, sizeof str, stdin);
fputs(str, fptr);
fclose (fptr);
fptr = fopen (fname, "r");
printf("\n The content of the file %s is :\n",fname);
str1 = fgetc(fptr);
while (str1 != EOF)
9
{
printf ("%c", str1);
str1 = fgetc(fptr);
}
printf("\n\n");
fclose (fptr);
return 0;
}
OUTPUT:
10
Q.2.
Problem Statement:
Write a Program in C to create a text file (abc.txt) containing a paragraph and count number of
characters, words, lines in that file and print on the console.
Algorithm:
Step1:start
Step2:declaring the main()
Step3:declaring a file using pointer and 2 character array and a character and 3 integer
type variables
Step4:declaring the name of the file and creating a file in writing mode
Step5:writing the sentences as input from user and storing them in the file
Step6:opening the file in reading mode
Step7:counting the number of words lines and characters and displaying them
Step8:end of main()
Step9:end
C Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100], sentence[1000];
char ch;
int characters, words, lines;
printf("Enter the name of file : ");
scanf("%s", path);
fflush(stdin);
file = fopen(path, "w");
if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
11
}
printf("Enter a paragraph:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(file, "%s", sentence);
fclose(file);
file=fopen(path,"r");
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
if (ch == '\n' || ch == '\0' || ch == '.')
lines++;
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
fclose(file);
return 0;
}
OUTPUT:
12
Q.3.
Problem Statement:
Write a Program in C to read N numbers from a file (abc.txt), sort and store into the same file.
Algorithm:
Step1:start
Step2:declaring a file using pointer and a character array and integer variables and an
integer array
Step3:taking the file name from user
Step4:opening the file in read mode and entering the data and storing them in an array
Step5:sorting the data in ascending order
Step6:displaying the sorted data in the file
Step7:end of main()
Step8:end
C Code:
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE*fptr;
char s[100];
int i=0,j,n=0,min,index,val[10],temp;
printf("Enter the file name: ");
gets(s);
fptr=fopen(s, "w");
printf("Enter the data: ");
for (i=0;i<5;i++)
{
scanf("%d",&val[i]);
fprintf(fptr,"%d ",val[i]);
}
printf("numbers generated successfully !! \n\n");
for (i = 0; i < 5-1; i++)
{
for (j = 0; j < 5-i-1; j++)
13
{
if (val[j] > val[j+1])
{
int temp = val[j];
val[j] = val[j+1];
val[j+1] = temp;
}
}
}
printf("Sorted list: \n");
for (i=0;i<5;i++)
{
printf("%d ",val[i]);
fprintf(fptr,"%d ",val[i]);
}
fclose(fptr);
return 0;
}
OUTPUT:
14
15
16