Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Palindrome Number Checking and Bubble Sort Program in C, Exercises of Computer Networks

AlgorithmsData StructuresProgramming

The code and instructions for two programming experiments conducted in the Computer Programming Laboratory during Semester-I of the 15PCD13 course at the Dept. of CSE, GCEM, Bangalore. The first experiment checks whether a given integer is a palindrome or not. The second experiment uses Bubble Sort to arrange a given set of integers in ascending order.

What you will learn

  • How can you modify the palindrome number checking program to work with strings?
  • Can you explain how the variables are used in the palindrome number checking program?
  • How does the palindrome number checking program work?
  • What is the time complexity of Bubble Sort?
  • What is the algorithm for Bubble Sort?

Typology: Exercises

2019/2020

Uploaded on 12/20/2022

shariar-shohan
shariar-shohan 🇧🇩

5 documents

1 / 56

Toggle sidebar

Related documents


Partial preview of the text

Download Palindrome Number Checking and Bubble Sort Program in C and more Exercises Computer Networks in PDF only on Docsity! APPROVED BY AICTE NEW DELHI, AFFILIATED TO VTU BELGAUM DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING COMPUTER PROGRAMMING LABORATORY LAB MANUAL - 15CPL16 SEMESTER-I/II 2016-2017 Prepared by: Reviewed by: Approved by: N.S.Saradha Devi N.S.Saradha Devi Dr. A.A. Powly Thomas Head of the Department Head of the Department Principal Dept. of CSE Dept. of CSE GCEM GCEM GCEM 81/1, 182/1, Hoodi Village, Sonnenahalli, K.R. Puram, Bengaluru, Karnataka-560048. Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 2 SEMESTER-I TABLE OF CONTENTS S. No Title of Contents Page From To 1 SYLLABUS 3 5 2 COURSE OBJECTIVE AND COURSE OUTCOME 6 6 3 COMPUTER LAB DO’S AND DON’TS 7 7 4 LIST OF EXPERIMENTS 8 56 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 5 SEMESTER-I 9. Write and execute a C program that i. Implements string copy operation STRCOPY(str1,str2) that copies a stringstr1to another string str2 without using library function. ii. Read a sentence and print frequency of vowels and total count of consonants. 10.a.Design and develop a C function RightShift(x ,n) that takes two integers x and n as input and returns value of the integer x rotated to the right by n positions. Assume the integers are unsigned. Write a C program that invokes this function with different values for x and n and tabulate the results with suitable headings. b.Design and develop a C function isprime(num) that accepts an integer argument and returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this function to generate prime numbers between the given range. 11. Draw the flowchart and write a recursive C function to find the factorial of a number, n!, defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C program to compute the binomial coefficient nCr. Tabulate the results for different values of n and r with suitable messages. 12. Given two university information files “studentname.txt” and “usn.txt” that contains students Name and USN respectively. Write a C program to create a new file called “output.txt” and copy the content of files “studentname.txt” and “usn.txt” into output file in the sequence shown below . Display the contents of output file “output.txt” on to the screen. 13. Write a C program to maintain a record of n student details using an array of structures with four fields (Roll number, Name, Marks, and Grade). Assume appropriate data type for each field. Print the marks of the student, given the student name as input. 14. Write a C program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of n real numbers. Student Name USN Name 1 USN1 Name 2 USN2 …. …. …. …. Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 6 SEMESTER-I COURSE OBJECTIVE  To provide basic principles C programming language.  To provide design & develop of C programming skills.  To provide practical exposures like designing flowcharts, algorithms, how to debug programs etc. COURSE OUTCOME  Gaining the Knowledge on various parts of a computer.  Able to draw flowcharts and write algorithms  Able to design and development of C problem solving skills.  Able to design and develop modular programming skills.  Able to trace and debug a program Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 7 SEMESTER-I COMPUTER LAB DO’S AND DON’T DO’S 1. Know the location of the fire extinguisher and the first aid box and how to use them in case of an emergency. 2. Read and understand how to carry out an activity thoroughly before coming to the laboratory. 3. Report fires or accidents to your lecturer/laboratory technician immediately. 4. Report any broken plugs or exposed electrical wires to your lecturer/laboratory technician immediately. DON’TS 1. Do not eat or drink in the laboratory. 2. Avoid stepping on electrical wires or any other computer cables. 3. Do not open the system unit casing or monitor casing particularly when the power is turned on. Some internal components hold electric voltages of up to 30000 volts, which can be fatal. 4. Do not insert metal objects such as clips, pins and needles into the computer casings. They may cause fire. 5. Do not remove anything from the computer laboratory without permission. 6. Do not touch, connect or disconnect any plug or cable without your lecturer/laboratory technician’s permission. 7. Do not misbehave in the computer laboratory. Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 10 SEMESTER-I FLOWCHART: F T T F F F T T Is a=0 & b=0? Print “Invalidroots” Is a=0? start Read a,b,c Disc <-b*b-4*a*c Is disc = 0? Is disc > 0? Print “Real and distinct roots” Print “imaginary roots” Print “Real & equal roots” Print “linear equation” Root 1 = -c/b Root 1 = (-b+sqrt(disc))/(2*a) Root 1 = (-b-sqrt(disc))/(2*a) Root 1 = -b/(2*a) Root 2 = root 1 Real = -b/(2*a) Image = sqrt(f abs(disc))/(2*a) Print Root 1 = real+ I imag Root 2 = real- I imag Print root 1, root 2 Print root1 stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 11 SEMESTER-I PROGRAM : /* Program to calculate all possible roots of a quadratic equation */ #include<stdio.h> #include<conio.h> #include<math.h> int main() { float a, b, c, disc; float root1,root2,real,imag; clrscr(); printf("Enter a,b,c values\n"); scanf("%f%f%f",&a,&b,&c); if( (a == 0) && (b == 0) &&(c==0)) { printf("Invalid coefficients\n"); printf(" Try Again with valid inputs !!!!\n"); getch(); } disc = b*b - 4*a*c; if(disc == 0) { printf("The roots are real and equal\n"); root1 = root2 = -b/(2*a); printf("Root1 = %.3f \nRoot2 = %.3f", root1,root2); } else if(disc>0) { printf("The roots are Real and Distinct\n"); root1 = (-b+sqrt(disc)) / (2*a); root2 = (-b-sqrt(disc)) / (2*a); printf("Root1 = %.3f \nRoot2 = %.3f",root1,root2); } else { printf("The roots are Real and Imaginary\n"); real = -b / (2*a); imag = sqrt(fabs(disc)) / (2*a);//fabs() returns only numberignoring sign printf("Root1 = %.3f + i %.3f \n",real,imag); printf("Root2 = %.3f - i %.3f",real,imag); } getch(); } Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 12 SEMESTER-I EXPECTED OUTPUT: 1. Enter a, b, c values 0 0 1 Invalid coefficients Try Again with valid inputs!!!! 2. Enter a, b, c values 1 2 3 The roots are Real and Imaginary Root1 = -1.000 + I 1.414 Root2 = -1.000 – I 1.414 3. Enter a, b, c values 1 2 1 The roots are real and equal Root1 = -1.000 Root2 = -1.000 4. Enter a, b, c values 1 5 3 The roots are Real and Distinct Root1 = -0.697 Root2 = -4.303 5. Enter a, b, c values 0 1 2 Linear equation Root = -2.000 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 15 SEMESTER-I printf("\n%d is not a palindrome", temp); getch(); } EXPECTED OUTPUT: 1. Enter the number 1001 The reverse number is 1001 1001 is a palindrome 2. Enter the number 123 The reverse number is 123 123 is not a palindrome Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 16 SEMESTER-I Experiment No. 3(A) Date: SQUARE ROOT AIM: To find Square Root of a given Number. ALGORITHM: SQUARE ROOT [this algorithm takes an integer number as an input and prints the square root of the number] Input: the integer number Output: print the square root number Step1: Initialize Step2: Enter the number Step3: if(n>=0) then Print the square root result Else Print it is not a valid number Step4: stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 17 SEMESTER-I FLOWCHART: True False False True False True PROGRAM : stop X<- x-0.001 Print ‘squrate root’ Is (x*x)> (double)n? X<- (double)s+d Decrement s For d=0.001, 0.002….to d<1.0 start Read n For s=1,2,….to s*s<=n Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 20 SEMESTER-I PROGRAM : /* Program to find whether given year is leap year or not */ #include<stdio.h> #include<conio.h> int main() { int year; clrscr(); printf("Enter valid year\n"); scanf("%d",&year); if((year%4==0) && (year%100!=0) || (year%400 ==0)) //check for leap year { printf("%d is leap year", year); } else { printf("%d is not a leap year",year); } getch(); } EXPECTED OUTPUT: 1. Enter valid year 2012 2012 is leap year 2. Enter valid year 1900 1900 is not a leap year Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 21 SEMESTER-I Experiment No.4 Date: Evaluation of Polynomial AIM: To find the value of the polynomial Design and develop an algorithm for evaluating the polynomial f(x) = a4 x4 + a3 x3 + a2 x2+ a1 x + a0, for a given value of x and its coefficients using Horner’s method. ALGORITHM : ALGM: EVAL_POLYNOMIAL f(x) = a4x4+ a3x3+ a2x2+ a1x+ a0using Horner’s method Steps: 1. [Initialize] Start 2. [Input the number of coefficients n] read n 3. Set sum to 0 4. [Read all coefficients a1, a2, a3, a4and constanta0] For each value i in array a(i)do read n+1 coefficients endfor 5. [Input variable x] read x 6. [Iterate from n to 0. Calculate each term a4x4, a3x ,a2x2,a1x, a0 . ] For each value iin array a(i) do sum ← sum * x + a[i] endfor 7. Print sum 8. [Finished] End Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 22 SEMESTER-I FLOWCHART: false true false true Sum = sum+a[0] Write sum stop Read x Sum = (sum+a[i]*x) For(i=n-1; i>0; i--) Sum = a[i]*x start for(i=0; i<=n; i++) Read n Read a[i] Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 25 SEMESTER-I FLOWCHART: False true True false nume –nume*x*x deno <- deno*(i*(i-1)) term <- (nume/deno) Is term < 0.001? Sum <- sum+term Print calculated sine, predefined sine values stop start Read degree PI <- 3.142 X <- degree*(PI/180) Nume <- x Deno <- 1 Sum <- x For i = 3,5,7,…to n Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 26 SEMESTER-I PROGRAM: /* Program to calculate sine value of given angle */ #include<stdio.h> #include<conio.h> #include<math.h> #define PI 3.142 int main() { int i, degree; float x, sum=0,term,nume,deno; clrscr(); printf("Enter the value of degree"); scanf("%d",&degree); x = degree * (PI/180); //converting degree into radian nume = x; deno = 1; i=2; do { //calculating the sine value. term = nume/deno; nume = -nume*x*x; deno = deno*i*(i+1); sum=sum+term; i=i+2; } while (fabs(term) >= 0.00001); // Accurate to 4 digits printf("The sine of %d is %.3f\n", degree, sum); printf("The sine function of %d is %.3f", degree, sin(x)); getch(); } EXPECTED OUTPUT: 1. Enter the value of degree 0 The sine of 0 is 0.000 The sine function of 0 is 0.000 2. Enter the value of degree 45 The sine of 45 is 0.707 The sine function of 45 is 0.707 3. Enter the value of degree 90 The sine of 90 is 1.000 The sine function of 90 is 1.000 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 27 SEMESTER-I Experiment No.6 Date: BUBBLE SORT AIM: To arrange given N integers in ascending order using Bubble Sort. ALGORITHM : ALGM: Bubble Sort [ This algorithm takes a list of unordered numbers and arrange them in ascending order using Bubble Sort method] Steps: 1. [Initialize] Start 2. [Input number of elements] read n 3. [Input unsorted elements in array] read elements in array a[ ] 4. print elements of array a[ ] 5. [Iterate array a[ ] in two loops. Outer loop gives number of passes. Inner loop does swap t ask.In each pass, compare each pair of adjacent items. If former element is greater than latter one, swap them.] [Iterate array a[ ] with for each value i in array a[i] to n do for each value j in array a[j] to n-1 do [Compare each pair of adjacent elements] if (a[j] > a[j+1])then [Swap these elements using temp variable] temp ← a[j] a[j] ← a[j+1] a[j+1] ← temp endif endfor endfor 6. Print array with sorted elements 7. [Finished] End. Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 30 SEMESTER-I Experiment No.7 Date: MATRIX MULTIPLICATION AIM: To read two matrices A(m x n )and B(p x q) and Compute the product A and B. ALGORITHM: ALGM: matrix multiplication Step 1: GET THE MATRIX SIZE OF a Input the size of matrix a and read the values of m and n. Step 2: GET THE MATRIX VALUE OF a For i=0 to n-1 For j=0 to n-1 Read a[i][j] End For End For Step 3: GET THE MATRIX SIZE OF b Input the size of matrix b and read the values of p and q. Step 4: GET THE MATRIX VALUE OF b For i=0 to p-1 For j=0 to q-1 Read b[i][j] End For End For STEP :5 MULTIPLICATION OF MATRICES NOT POSSIBLE If(n!=p) Print(“multiplication is not possible”) Stop End if STEP 6: MULTIPLICATION OF MATRICES IS POSSIBLE For i=0 to m-1 For j=0 to q-1 Sum=0 For k=0 to n-1 Sum=Sum+a[i][k]*b[k][j] End for End for Step 7: DISPLAY RESULT For i=0 to m-1 For j=0 to q-1 Print c[i][j] End for End for Step 8: STOP stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 31 SEMESTER-I PROGRAM : #include<stdio.h> #include<conio.h> int main() { int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k; clrscr(); printf("Enter the size of first matrix\n"); scanf("%d %d",&m,&n); printf("Enter the size of second matrix\n"); scanf("%d %d",&p,&q); if(n!=p) printf(“Matrix multiplication is not possible”); else { printf("Enter the elements of first matrix\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter the elements of the second matrix\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++) for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } printf("\n A- matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d\t",a[i][j]); printf("\n"); } printf("\n B- matrix is\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) printf("%d\t",b[i][j]); printf("\n"); } printf("The product of two matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) printf("%d\t",c[i][j]); Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 32 SEMESTER-I printf("\n"); } } getch(); } EXPECTED OUTPUT: 1. Enter the size of first matrix 2 3 Enter the size of second matrix 3 2 Enter the elements of first matrix 1 2 3 4 5 6 Enter the elements of the second matrix 1 2 3 4 5 6 A- matrix is 1 2 3 4 5 6 B- matrix is 1 2 3 4 5 6 The product of two matrix is 22 28 49 64 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 35 SEMESTER-I Experiment No: 9 (A) Date: STRING COPY AIM: TO execute a C program that Implements string copy operation STRCOPY(str1,str2) that copies a string str1 to another string str2 without using library function. ALGORITHM: ALGM: EVAL_POLYNOMIAL [To copy string i/p to its o/p, replacing each string of one or more blanks by a single blank]. Inputs: str1 and str2. Output: str1 -> str2. 1.Start 2. Read the text in Array c 3. FOR i← 0 to c [i] <> '\0' in steps of 1 IF c [i] = ' ' Print ' ' End If Until c [i] = ‘\0' Increment i End Until Print the character End For 4. Stop FLOWCHART: String matched string not matched start Enter the 2 string While(s[i]!=’\0’) Print the string is valid Print the string is not valid stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 36 SEMESTER-I Program : /* program to copy a string without using library function */ #include<stdio.h> #include<conio.h> // function to copy a string void strcopy(char s1[50], char s2[50]) { int i=0; while(s[1i]!='\0') { s2[i]=s1[i]; i++; } s2[i]='\0'; } int main() { char str1[50],str2[50]; clrscr(); printf("Enter the source string\n"); gets(str1); strcopy(str1,str2); printf("Destination string is\n"); puts(str2); getch(); } OUTPUT: 1. Enter the source string Drsmce Destination string is Drsmce 2. Enter the source string 1234 Destination string is 1234 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 37 SEMESTER-I Experiment No. 9(B) Date: VOWELS AND CONSONANTS COUNT AIM: To Read a sentence and prints frequency of each of the vowels and total count of consonants. ALGORITHM: ALGM: VOWELS AND CONSONANTS COUNT[To read a sentence and prints frequency of each of the vowels and total count of consonants.] Input: Sentence. Output: Print vowels(a,e,i,o,u) ,consonants(other than vowels) and print their count. STEP 1.Set countCo:=0[C is the counter variable for consonants] STEP 2.Set countVo:=0[counter for vowels ] STEP 3.[find length of STR] Set L:=LENGTH(STR). STEP 4:TOLOWER(STR,L) [change all characters in string to lower case] STEP 5. Repeat for K:=0 to L-1: If STR[K]='a' OR STR[K]='e' OR STR[K]='i' OR STR[K]='o' OR STR[K]='u'.Then: Set countVo:=countVo+1 Else Set countCo:=countCo+1 [End of If-Else] [End of for loop] STEP 6.PRINT countVo,countCo [display results] STEP 7.EXIT/END FLOWCHART: If it consists vowels otherwise start Enter the sentences For(i=0; i<strlen(s); i++) If is alpha(s[i]) Print the number of vowels Print the number of consonants stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 40 SEMESTER-I Flowchart: True false Program : /* program to rotate right */ #include<stdio.h> #include<conio.h> #include<math.h> //function to right rotate unsignedint RightShift(unsignedint x,unsignedint n) { int i; for(i=0;i<n;i++) { if(x%2==0) x=x>>1; else { x=x>>1; x=x+32768; } } return(x); } void main() { unsigned int x,y,n,i,value; char input; clrscr(); start Enter x and n For(i=0; i<n; i++) If(x%2==0) X=x>>1 X=x>>1 X=x+32768 stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 41 SEMESTER-I do { printf("\nEnter the number and the no of bits to be” “rotated\n"); scanf("%u %u",&x,&n); y=x; value=RightShift(x,n); printf("\nCALULATED VALUE rightrot of %u,%u=%u",y,n,value); printf("\nTocontinue press 'y' else press 'n'\n"); input=getche(); }while(input!='n'); getch(); } Output: 1. Enter the number and the no of bits to be rotated 4 1 CALULATED VALUE rightrot of 4,1=2 To continue press 'y' else press 'n' y Enter the number and the no of bits to be rotated 5 2 CALULATED VALUE rightrot of 5,2=16385 To continue press 'y' else press 'n' Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 42 SEMESTER-I Experiment No: 10(b) Date: TO CHECK PRIME OR NOT 10 ii). Design and develop a function isprime (x) that accepts an integer argument and returns 1 if the argument is prime and 0 otherwise. The function must use plain division checking approach to determine if a given number isprime. Invoke this function from the main with different values obtained from the user and print appropriate messages Algorithm: ALGM: CHECK PRIME OR NOT [A function isprime (x) that accepts an integer argument and returns 1 if the argument is prime and 0 otherwise.] Input : Any integer number (up to 32767) Output: Is it a prime number or Not Complexity O(n) Prime(num) 1 Set i=2 2 while i<=num/2 3 if num mod i = 0 4 print "Not a Prime number" and exit; 5 i=i+1 6 if (i==(num/2)+1) 7 Print "Prime number" Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 45 SEMESTER-I Experiment No: 11 Date: FACTORIAL OF NUMBER USING RECURSIVE FUNCTION Draw the flow chart and write a Recursive C function to find the factorial of number n! defined by fact(n)=1, if n=0, otherwise fact(n)=n*fact(n-1),using this function write a c program to compute the binomial co-efficient nCr. Tabulate the results for different values of n and r using suitable messages. Algorithm: ALGM: Factorial of number [A recursive C function to find the factorial of number n! defined by fact(n)=1, if n=0, otherwise fact(n)=n*fact(n-1)] FUNCTION FACTORIAL (N: INTEGER): INTEGER (* RECURSIVE COMPUTATION OF N FACTORIAL *) BEGIN (* TEST FOR STOPPING STATE *) IF N <= 0 THEN FACTORIAL := 1 ELSE FACTORIAL := N * FACTORIAL(N - 1) END; (* FACTORIAL *) Flowchart T F Return 1 Return fact(n-1) stop start Enter n, r res = fact(n)/(fact(n-r)*fact(r)) If n<=0 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 46 SEMESTER-I Program : #include<stdio.h> #include<conio.h> int fact(int n) { if(n==0) { return 1; } return (n*fact(n-1)); } int main() { int n,r,res; clrscr(); printf("Enter the value of n and r\n"); scanf("%d%d",&n,&r); res=fact(n)/(fact(n-r)*fact(r)); printf("The NCR is = %d",res); getch(); } Output: 1. Enter the value of n and r 4 2 The NCR is =6 2. Enter the value of n and 7 3 The NCR is =6 Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 47 SEMESTER-I Experiment No: 12 Date: COPY THE CONTENTS OF FILE Given two university information files "studentname.txt" and "usn.txt" that contains students names and USN respectively. Write a C program to a new file called "output.txt" and copy the content of files "studentname.txt" and "usn.txt" into output file in the sequence shows below. Display the content of output file "output.txt" on to the screen. Note : Students are required to create two files “Studname.txt” and “Studusn.txt” with the Contents studname.txt studusn.txt Amar 1RN10CS005 Suresh 1RN10CS012 Kiran 1RN10CS036 Shashi 1RN10CS072 ALGORITHM: Input : Create the file fp1,fp2,fp3 Output : Print whether the files are found or not step1:start Step2: create the files (fp1,fp2, & fp3) Step3: while(!feof((fp1)&&!feof(fp2)) Step4:Both files the not created the print the file not found a). if only one file is created then print files not found. b).if both files are created the print files are found. Step5: stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 50 SEMESTER-I Experiment No: 13 Date: STUDENT RECORD USING ARRAY OF STRUCTURES Write a C program to maintain a record of "n" student details sing an array of structures with four fields (Roll number, Name, marks, and Grade). Each field is of an appropriate data type. Print the marks of the student given name as input. ALGORITHM: Input: Enter the number of students Output: print the marks of the student. Step1: start Step2: enter the num of students Step3: if(strcmp(s[i].name, sname==0) Then { Print the marks of students name & USN } Else { Print the given date is invaid } Step4: stop Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 51 SEMESTER-I FLOWCHART: N=3 N=2 N=3 Program: /* program to maintain a record of student using structrue */ #include<stdio.h> #include<conio.h> struct student { int rollno, marks; char name[20], grade; }; Void main() { int i,n,found=0; struct student s[10]; char sname[20]; clrscr(); printf("Enter the number of student details n="); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nenter the %d student details \n",i+1); printf("enter the roll number:"); scanf("%d",&s[i].rollno); start stop Enter n Enter the details of 3 students Enter the details of 2 students Enter the details of 3 students but it will not display If(strcmp(s[i].name, sname)==0) Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 52 SEMESTER-I printf("enter the student name without white spaces:"); scanf("%s", s[i].name); printf("enter the marks : "); scanf("%d", &s[i].marks); printf("enter the grade : "); fflush(stdin); scanf("%c",&s[i].grade); } printf("\nStudent details are \n"); printf("\nRollno\tName\t\t\tMarks\tGrade\n"); for(i=0;i<n;i++) printf("%d\t%s\t\t%d\t%c\n", s[i].rollno, s[i].name, s[i].marks, s[i].grade); printf("\nEnter the student name to print the marks:"); scanf("%s", sname); for(i=0;i<n;i++) { if(strcmp(s[i].name,sname)==0) { Printf(“\Marks of the student is : %d”,s[i].marks); found=1; } } if(found ==0) printf(“ Given student name not found\n”); getch(); } Output: Enter the number of student details n=3 enter the 1 student details enter the roll number: 100 enter the student name without white spaces:vishwa enter the marks : 90 enter the grade : A enter the 2 student details enter the roll number: 101 enter the student name without white spaces:madhu enter the marks : 50 enter the grade : B enter the 3 student details enter the roll number: 102 enter the student name without white spaces:kiran enter the marks : 45 enter the grade : C Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 55 SEMESTER-I FLOWCHART: Enter the values Enter the values in integers in floating point Program: #include<stdio.h> #include<conio.h> #include<math.h> int main() { float a[10], *ptr, mean, std, sum=0, sumstd=0; int n,i; clrscr(); printf("Enter the no of elements\n"); scanf("%d",&n); printf("Enter the array elements\n"); for(i=0;i<n;i++) { scanf("%f",&a[i]); } ptr=a; for(i=0;i<n;i++) { sum=sum+ *ptr; ptr++; } mean=sum/n; ptr=a; for(i=0;i<n;i++) { start stop for(i=0; i<n; i++) enter n Print the result in integer(sum, mean, sumstd) Print the result in floating point(sum, mean, sumstd) Computer Programming Laboratory 15PCD13 2016-2017 Dept. of CSE, GCEM, Bangalore Page 56 SEMESTER-I sumstd=sumstd + pow((*ptr - mean),2); ptr++; } std= sqrt(sumstd/n); printf("Sum=%.3f\t",sum); printf("Mean=%.3f\t",mean); printf("Standard deviation=%.3f\t",std); getch(); } Output: Enter the no of elements 5 Enter the array elements 5 3 9 8 7 Sum=32.000 Mean=6.400 Standard deviation=2.154
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved