Sie sind auf Seite 1von 11

C Programming CODE DEBUGGING 1. Observe the code below, and correct all the syntax errors: #include<stdio.

h> void main() {//char cvar='A'; switch(cvar) case 'E': case 'I': case 'O': case "U":puts("vowel"); default:puts("Not an vowel"); } } char cvar; printf('\nEnter a character : "); scanf("%c",cvar);

{ case 'A':

2. Go through the code and display the message using each of the methods. Check and correct errors #include<stdio.h> void main() { int cont; /* Method 1: printf("\n c language "); printf("\nc language"); printf("\n c language "); */ // Method 2 : while while(cont<=5) { printf("\n c language "); cont++; // Method 3 : do while /* do { */ printf("\ nc language "); cont++; }while(cont<5); } printf("\nc language"); printf("\ nc language "); cont=1;

printf("\n c language ");

// Method 4 : for /* for(;cont<=5;cont++); printf("\ nc language "); cont++; */ printf("\n\n"); } 3. Go through the code and fill up the blanks in putchar to display the contents of character variables #include<stdio.h> void main() { char v1,v2,v3,v4,v5; v1='W'; v2='I'; v3='P'; v4='R'; v5='O'; // Method 1 : putchar(...); putchar(...); // Alternate Method : putchar('\n'); } putchar(...); putchar(...); putchar(...);

4. The following program accepts 5 characters and displays them. Correct errors if found in any #include<stdio.h> Page 5 of 16 #include<conio.h> void main() { char v1,v2,v3,v4,v5; printf("\nEnter 5 characters : ");

// Method 1 : using scanf() scanf("%d%d%d%d%d,&v1,&v2,&v3,&v4,&v5); // Method 2 : using getchar() /* getchar(v1); getchar(v2); getchar(v3); getchar(v4); getchar(v5); */ // Method 3 : using getche()

/* v1=getche();putchar('*'); v2=getche();putchar('*'); v3=getche();putchar('*'); v4=getche();putchar('*'); v5=getche();putchar('*'); */ // Method 4 : using getch() v1=getch();putchar('*'); v2=getch();putchar('*'); v3=getch();putchar('*'); v4=getch();putchar('*'); v5=getch();putchar('*'); // Display the characters printf("\nEntered characters are %c%c%c%c%c\n\n, &v1,&v2,&v3,&v4,&v5); }

5. Go through the program and specify the output and explanation of the result obtained #include "stdio.h void main() { int a,b; a = 1; b = (++a) + (a++); printf("\na=%d\tb=%d\n",a,b); printf("\n%d\t%d", a, a++); } EXERCISES 1. wap to accept a centigrade val and display its equiv in Fahrenheit. Hint: Fahrenheit = (9/5) * tc + 32; 2. wap to accept principal amount, rate of interest and duration. Calculate the simple interest and display all the values in a neat format. Hint: Simple Interest = p * t * r / 100 3. Write a program to accept three numbers and display the greatest number. 4. wap to accept a number from 1 to 7. Display the weekday with 1 represented as Sunday. 5. wap to read in 10 numbers and compute the average, maximum and minimum values. 6. wap to display an electricity bill. Accept the previous meter reading, current meter reading. Calculate the units consumed. Calculate the bill amount as per the following criteria: Units Price 0 200 Rs.1/ unit 201 400 Rs.2/ unit >400 Rs.5/ unit 7. Write a solution to find the income tax of an employee Input: total taxable income of an employee, Employee ID Output: Total tax on income Gross taxable Salary range Tax percentage

<=1,50,000 0% >150,000 and <=300000 10% >300000 and <=500000 20% >500000 30% 8. Write a program to accept the marks percentages of 10 students. Display the marks and count of students who have scored above the average. 9. wap to accept a name and display the no: of vowels and consonants. Also display the name vertically. 10. wapto accept values into a 2 dimensional array with 5 rows and 5 columns and display the values in the form of rows and columns. Display the row total and column total for each row and column. ADDITIONAL EXERCISES 1. Find first n prime numbers 2. Find the sum of 1+22+32+-----------+n2 3. Find biggest of three numbers 4. Find whether given year is leap year or notPage 7 of 16 5. Find reverse of a number n 6. wap to read an inr value. Assuming it is the no: of a month of the year; print out name of that month 7. Write a program to accept the valid marks scored by a student in 5 subjects. Calculate the marks percentage and find the result as per the following criteria: Marks Result 70 100 Distinction 60 69 I Class 50 59 II Class 35 49 III Class 0 34 FailPage 8 of 16 C Programming Part-2 This section covers code debugging examples and exercises on topics: Pointers in C, Functions in C, String Handling Functions and Storage Types. CODE DEBUGGING

1. Go through the program and correct the errors to display the arrays #include <stdio.h> void main() { int arr[10]={0},i,j; // initializes all locations to 0 int arr1[10]={5,2}; // first two are 5, 2 rest are zeros printf("\nArray : arr \n"); for(i=0;i<9;i++) printf("%d\n",arr[j]); printf("\nArray : arr1 \n"); for(j=0;j<=9;j++) printf("\n%d",arr1[i]); printf("\n\n"); } 2. Go through the program and fill up the blank with an appropriate statement. #include <stdio.h> void main() { char names[5][30]; int i; // Accepting the names from the user for(i=0;i<=4;i++){ printf("\nEnter name of %d participant : ",i+1); } //Displaying the names for(i=0;i<=4;i++) printf("\n\n"); } 3. Go through the program and correct the logical errors in the code. #include<stdio.h> int area(int,int); void main() { int l,b,a; printf("\nEnter length : "); scanf("%d",&l); printf("\nEnter breadth : "); scanf("%d",&b); area(l,b); printf("\nLength:%d\tBreadth:%d\n",l,b); printf("\nArea is : %f\n\n",a); } int area(int l,int b) { int a; a=l*b; l=100; }

4. Go through the code below and suggest the statements to be used to make the pointers p1 and p2 to point to str1 and str2 and correct the errors if any. #include<stdio.h> void main() { char str1[20],str2[20],*p1,*p2; puts("\nEnter a string"); gets(str1); ................; ................; while(*p1) { *p2=*p1; p1++; p2++; } *p2="\0"; puts(str2[1]); } 5. Go through the code and correct the code to display the array in matrix form. #include<stdio.h> Page 10 of 16 void main() { int a[3][5]={{2,4,6,8,10},{3,6,9,12,15},{5,10,15,20,25}}; int i,j; printf("\n 2row, 3rd column value is %d,*(*(a+2)+3)); printf("\n 3rd row,5 column value is %d ",*(*(a+3)+5)); printf("\n\n"); for(i=0;i<=2;i++) for(j=0;j<=4;j++) printf("%d\t", *(*(a+j)+i)); } printf("\n\n"); } EXERCISES 1. Write a program to accept marks scored by 10 students. Using a pointer, display the total size allocated to the array. Copy the contents of array into another array in sorted way. 2. Write a function to accept 2 nos swap them using a function and display the swapped values. 3. Write a program to accept an array of ten numbers. Accept a search value from the user. Pass the array and the search value to a function searchArray(). If the value is found in the array, return the position otherwise return -1 to indicate that the value is not found. 4. Write a program to accept the following details through command line arguments: 1. Emp id, Ename, Department, Salary

2. Display the number of parameters passed along with the following: Empid, Ename, Department, Salary and Annual salary 3. Please ensure that 4 parameters are passed. If the number of parameters passed is less than 4 or greater than 4 then display an error message. 5. Write a program to create a user defined function to concatenate two strings. ADDITIONAL EXERCISES 1. Write a program to accept 10 values into the array and display it in reverse order. 2. wapto accept 10 values into an array and display no: of even values and no: of odd values in the array. 3. wapto accept a string and display whether it is a palindrome or not. Hint: palindrome: Malayalam 4. Write a program to accept a string. Display the frequency of occurrence of every letter. 5. Write a program to accept the marks scored by 5 students in 6 subjects. Display the marks along with the total and percentage for each student. 6. Write a program to accept 5 names and display duplicate names. 7. Write a program to accept two strings and perform the following: 1. Concatenate two strings and display the concatenated string 2. Compare the two strings, if they are equal, display equal otherwise not equal 8. Write a program to record the marks scored by 5 students in 6 subjects, using a pointer to the array. Display the number of students who have scored above average. 9.wap to accept two numbers. Display the sum of the two numbers using recursive function. 10. Write a program that prompts the user for a string, and prints its reverse. 11. Write a program to add two 4 X 4 matrices. 12. Write a program to interchange the values of two variables using pointers.Page 12 of 16 This section covers code debugging examples and exercises on topics: User Defined Data types and File Input Output functions. CODE DEBUGGING 1. Go through the code to display the weekdays. Enter the increment step in the for loop. Enter a statement to display the contents of "d" within the for loop. #include <stdio.h>

enum weekday { sun,mon, tue, wed,thu,fri,sat}; void main() { enum weekday d; for(d=(enum weekday)sun;d<=sat;.......) { ........................ (1)//display contents of d switch(d) { case sun:printf("\nSunday\n\n");break; case mon:printf("\nMonday\n\n");break; case tue:printf("\nTuesday\n\n");break; case wed:printf("\nWednesday\n\n");break; case thu:printf("\nThursday\n\n");break; case fri:printf("\nFriday\n\n");break; case sat:printf("\nSaturday\n\n");break; }}} 2. Go through the code below and fill the blank lines with appropriate statements #include<stdio.h> struct k { int v; char *s; char name[10]; char v1[10]; float f; }; void main() { struct k v1; //Initialize the array with the following values: ................... // 10 for v ................... // "Purchase" for s ................... // "Sunil" for name ................... // "Manager" for v1 ................... // 56000 for f //Display the size of the struct k

printf("\n%d\n\n", sizeof(struct k)); ................... // Display structure } 3. The program below creates the file with as many names as the user wants and displays the names from the file. Go through the code and correct the errors(syntax / logical) #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> void main() { FILE *fp; char n[20],p[20]; char ans='y'; //opening the file for creation fp=fopen("NamesList.txt","a"); if(fp==NULL) { puts("\nError "); exit(1); } 4. The program below creates the file with as many names as the user wants and displays the names from the file. Go through the code and correct the errors(syntax / logical) #include <stdio.h> #include <stdlib.h> #include <conio.h>Page 14 of 16 #include <string.h> void main() { FILE *fp; char n[20],p[20]; char ans='y'; //opening the file for creation fp=fopen("NamesList.txt","a"); if(fp==NULL) { puts("\nError "); exit(1); } } 5. The code below creates a file with booklist. Fill the blank lines where fwrite() and fread() functions are used to write and read books from the file

#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> struct Book { int bookid; char title[20]; float price; }; void main() { struct Book b1,b2; char ans='y'; FILE *fp; fp=fopen("d:\Booklist.txt","a+"); if(fp==NULL) { puts("\nError "); exit(1); } while(ans=='y'||ans=='Y') { printf("\nEnter Book id : "); scanf("%d",&b1.bookid); fflush(stdin);Page 15 of 16 printf("\nEnter title : "); gets(b1.title); fflush(stdin); printf("\nEnter price : "); scanf("%f",&b1.price); fflush(stdin); ............................//fwrite function puts("\nAny more y / n ? "); ans=getche(); } fseek(fp,0,0); ...................... //fread function while(!feof(fp)) { printf("\n%d\t%d\t%s\t%f", ftell(fp),b2.bookid,b2.title,b2.price); .................... //fread function } fclose(fp); printf("\n\n"); } 6. Correct the program below to display the values correctly. #include <stdio.h> #include <string.h>

union Eg { int i; float f; char n[20]; }; void main() { union Eg u; u.i=10;u.f=6.589; strcpy(u.n,"Amit"); printf("\ni = %i",u.i); printf("\nf = %f",u.f); printf("\nn = %s\n\n",u.n); printf("%d\n\n",sizeof(u)); } 1. wap that will read 10 employees details such as employee no, name, job, salary &their department no., and store it in an array of structure. Also, search a particular employee details from the structure and display the employee details if it is present, else display No data found. 2. wapto store the vehicle details: Reg.no, brand, cc, mileage. Display the details of the vehicle. 3. Enhance the above program to accept 5 vehicles into an array. Display the details of the vehicle that gives a very good mileage. 4. Write a program to create a file to store the employee ids. Display the employee ids from the file along with the number of employee ids stored in the file. 5. Enhance the above program to append the file with a few more employee ids. Display the total count of employee ids available in the file along with employee ids from the beginning. 6. Write a program to copy the contents of one text file to another. 7. Write a program to store & read the data in a file using fwrite() and fread() functions. Use structure to read the data from user. 8. Write a program to eliminate the duplicate empids' from the list of employees in a file. ADDITIONAL EXERCISES 1. wap to create struct Book and define it as book, with the following elementsa. Isbn, title, author, price b. Perform the following:i. Create as many books as the user wants.ii. Store them in a file called: bookslist iii. Search for a specific book with isbn as specified by the user. If the book is found display the details otherwise display an error message.iv. Accept an isbn from the user to modify the details. If the book Is found, modify the details otherwise display an error message.v. Display the total number of books in the file : bookslist 2. WAP to read the text contents frm the file.display the total no: of chars,words,lines&size of file.