Department of Computer Science and Engineering M S Ramaiah Institute of Technology, Bangalore (Autonomous Institute Affiliated to VTU)
Fundamentals of Computing Lab Manual for I/II Semester Subject Code: CSL101
Reviewed By: Jayalakshmi Department of Computer Science and Engineering M S Ramaiah Institute of Technology, Bangalore (Autonomous Institute Affiliated to VTU)
Fundamentals of Computing Lab Manual Reviewed By: Jayalakshmi D. S. Department of Computer Science and Engineering
Dept. of Computer Science & Engineering, MSRIT Page 2
List of Exercises for FOC Lab, Sep-Jan 2012
Sl.No Exercises 1
1. Create a document using a suitable word processing package, like MS Word, with at least three paragraphs and perform editing operations. 2. Create a formal letter using a suitable word processing package, like MS Word, to place a purchase order for procurement of books. Use tables for list of books. 3. Create a Spreadsheet in MS-EXCEL and enter the marks of a student, calculate total and print grade if the student has passed in all subjects. 4. Create a Power-point presentation with at least 6 slides. 2
1. Write a C program to find area of a rectangle. 2. Write a C program to find area and circumference of a circle by defining the value of PI. 3. Write a C program to swap 2 variables without using temporary variable. 4. Write a C program to find simple interest. 5. Write a C program to find area of a triangle, given its sides. 3
1. Write a C program to find check whether the number is positive or negative. 2. Write a C program to find check whether the number is odd or even. 3. Write a C program to check whether the triangle is isosceles, equilateral or scalene using if-else. 4. Write a program to check whether a given year is a leap year. 5. Write a C program to find the biggest of three numbers.
4
1. Write a C program to find grade based on percentage using else if statement. 2. Write a C program to perform a desired arithmetic operation using switch statement and declaring choice as char data type. 3. Write a C program to find roots of a quadratic equation using switch. 4. Write a C program to find area of a triangle/square/circle/rectangle using switch statement.
5 1. Write a C program to demonstrate use of Size of operator. 2. Write a C program to find whether a entered character is a number or digit using both ASCII values and built-in functions. 3. Write a C program to convert a lower case alphabet to uppercase and vice-versa using both ASCII values and built-in functions. 4. Write a C program to find sum of odd numbers and even numbers in first n natural numbers 5. Write a C program to find factorial of a given number.
Dept. of Computer Science & Engineering, MSRIT Page 3
Sl.No Exercises 6
1. Write a C program to find GCD and LCM of two numbers. 2. Write a C program to find sum of digits of a given number 3. Write a C program to find whether a given number is palindrome or not. 4. Write a C program to print first N fibonacci numbers. 5. Write a C program to print prime numbers within given range. 6. Write a C program to to find sine series x - x^3/3! + x^5/5! - .... upto N terms accuracy.
7 1. Write a C program to find sum of all elements of an array. 2. Write a C program to search for an element in array using linear search. 3. Write a C program to search for an element in array using binary search. 8
1. Write a program to find length of string without using built-in functions. 2. Write a program to copy from one string to another without using built-in functions. 3. Write a program to combine two strings without using built-in functions. 4. Write a program to compare two strings without using built-in functions.
9
1. Write a program to find reverse of a string without using built-in functions. 2. Write a program to find whether given string is palindrome or not without using built-in functions. 3. Write C Program with some built-in functions of a string. 4. Write a C program to add two matrices.
10
1. Write a C program to find trace and norm of a given matrix. 2. Write a C program to multiply two matrices. 3. Write a C program to sort elements in ascending order using bubble sort with functions.
11
1. Write a C program to multiply two matrices using functions. 2. Write a program to determine the length of a character string using pointers. 3. Write a program to input N student information such as name, id, percentage. Display the information of the student with the highest percentage using array of structures.
12
1. Write a program to illustrate use of pointers in expressions. 2. Write a program to compute sum of all elements stored in an array using pointers 3. Write a program to swap two numbers using pointers. 4. Write a program to illustrate function returning pointer.
Dept. of Computer Science & Engineering, MSRIT Page 4
Lab Exercise: 1
a) Create a document using a suitable word processing package, like MS Word, with at least three paragraphs and perform editing operations.
The increasing popularity of C is probably due to its many desirable qualities. It is a robust language whose rich set of built-in functions and operators can be used to write any complex program. The C complier combines the capabilities of an assembly language with the features of a high-level language and therefore it is well suited for writing both system software and business packages. Programs written in C are efficient and fast. This is due to its variety of data types and powerful operators. It is many times faster than BASIC. There are 32 keywords in ANSI C and its strength lies in its built-in functions. C is highly portable. This means that C programs written for one computer can be run on another with little or no modification. In fact, many of the C compliers available in the market are written in C.
Features of C: C language is well suited for structured programming. C program is basically a collection of functions that are supported by the C library. With the availability of a large number of functions, the programming task becomes simple. The modular structure makes program debugging, testing and maintenance easier.
Steps: Opening Word 1. Click Start button, then go to programs submenu and then select Microsoft word. In some computers Microsoft Word could be inside Microsoft Office sub menu as it is a part of larger package called MS Office. 2. Press Enter, Word will open and you will get the message Microsoft Word Document 1 Edit. The Word Screen 1. Like all applications in Windows the program window of MS Word contains the Title bar at the top followed by the Menu bar, the toolbars, one or more toolbar, formatting bar, the document window and the status bar at the bottom. There also could be ruler bar below the formatting bar. 2. This complete program window can appear in any of three sizes on the screen 3. Restore: It covers 2/3 rd part of the screen 4. Maximize: It covers the complete screen except the area of the task bar.
Dept. of Computer Science & Engineering, MSRIT Page 5
5. Minimize: It appears as an icon in the taskbar. 6. It is important to note that the taskbar is always visible. 7. Title bar: The TITLE BAR appears at the top of the window and it contains the name of the document followed by the name of the program. At the right hand corner of the bar there are three buttons: Minimize, Maximize/Restore and Close button.
Entering text 1. It is done through the alpha numeric keys on the key board. 2. The text gets inserted to the left of where Insertion pointer is positioned on the screen. The pointer keeps moving towards the right as more and more text is inserted. The insertion pointer can be identified as a blinking I-beam shape on the document window. If it is not blinking then no text can be inserted. Saving a document 1. First decide a file name and location; that is, in which drive and/or folder you want to save this file. 2. Select SAVE AS..from FILE menu and press ENTER. 3. When a existing document needs to be saved with a different name, different format or at a different location then only SAVE AS.. can be used. Editing text 1. In the above three paragraphs, some words and lines are been edited with the help of editing functions that are available on the sub menu bar. They are: Each new line of the paragraph is started from fresh line with leaving some space at the prefix so that it will make easier to understand a new paragraph. Two lines have been written in Italics(I) style with the help of Font dialogue box to highlight the importance of C language. Two words are been colored with the help of Font Color to highlight the important languages used. When you find some of the words underlined with zigzag green , it means some spelling mistake, space or signs. Some words are been in written in capital and made bold to highlight the differences between the two different functions using Bold(B)s option from the Font dialogue box. To make more clear of the document or text Underlines are used using Underline(U) option from the Font dialogue box. The whole paragraph is aligned using various options from the Paragraph dialogue box like; align text left, centre, right or both left &right margins to make the document more neat and easy to read. There are bullets and numbers used from the paragraph dialogue box to note some of the important points and reduce the length of the paragraphs and make it easy to remember.
Dept. of Computer Science & Engineering, MSRIT Page 6
Closing a document 1. Go to file menu and press ENTER on CLOSE to close the document which is active.
b) Create a formal letter using a suitable word processing package, like MS Word, to place a purchase order for procurement of books. Use tables for list of books.
From, Date: 16th Oct 2012 M S Ramaiah Institute of Technology Department of Computer Science & Engineering, Bangalore 01
To, The Manager XYZ Book Depot, West Patel Nagar, New Delhi 110008
Sir, Sub: Procurement of books. I kindly request you to send the following books that are mentioned in the below list to the above address at the earliest. The payment will be done as soon the delivery of the books is received.
Sl. no. Title of the book Details of the books No. of copies Author Edition Publishers 01 Programming in ANSI C E Balagurusamy 5 th Tata MC Graw- Hill Publishers co. ltd. 10 02 Let Us C Yashwanth Kanitkar 8 th PQR Publishers 10 03 Data Structures Using C Aaron M. Tenenbaum Yedidyah Langsam Moshe J. Augenstein 3 rd Pearson Education, Inc. & Dorling Kindersley Publishing Inc. 10
Thanking you,
Yours Faithfully, ABC
Dept. of Computer Science & Engineering, MSRIT Page
Steps: Open MS Word document. Type the letter to be sent to the desired address using Tab button to write the addresses. Using Align Text Right option, write date. Using Insert option from the main menu, insert a table with needed rows and columns to fill the information.
c) Create a Spreadsheet in MS-EXCEL and enter the marks of a student, calculate total and print grade if the student has passed in all subjects.
Steps: 1. Open MS Excel from MS Office 2. A blank sheet is open with number of Rows(in numbers) & Columns(in alphabets) 3. All kind of Calculations can be performed by using appropriate formulas 4. Graphical presentation of the information can also be presented using Insert option 5. When you open an MS Excel page, you can find 3 sheets already displayed on the 1st page.
Dept. of Computer Science & Engineering, MSRIT Page !
6. Here we have taken the example of a Marksheet of a student 7. The name & class of the student is mentioned in C4 &C5 ie, column C & row 4,5 8. Similarly the subjects & marks are displayed using B8,C8, D8 to B14,C14,D14 9. The total of Max marks is calculated using the formula =sum(C8:C15) 10. Similarly the total of Marks obtained is calculated using the formula =sum(D8:D15) 11. The percentage is calculated using the formula =sum(D17/C17)*100 12. The grade is obtained using the formula =If(D18>90,"S",If(D18>80,"A",If(D18>70,"B",If(D18>60,"C",If(D18>50,"D","F"))))) 13. When you hold the mouse on any of the calculations, you can find the formulas used to calculate on display bar below the submenus 14. You can use the font color to highlight any main notes, rows or columns by selecting the appropriate column name & row number.
d) Create a Power-point presentation with at least 6 slides.
Step 1: Go to start, then programs, and select Microsoft PowerPoint. Step 2: Click File, then new Step 3: Once you have a new presentation open, go to format and select slide layout. Select title slide , click on it to apply it to your first slide. Step 4: Click on the first block and type your name. Next click on the second block and type Black Family Technology Day. Step 5: Once you have finished entering information for your first slide. You are ready to add animation to your text, or picture. Step 6: Click on Box one. Next Click the slide show tab on the toolbar Select custom animation. Now click add effect, then click emphasis, and then click Grow/Shrink. Next you want to check the setting of your animation. Look where it says modify Grow/Shrink. Click the down arrow next to on click and select after previous. Next go to slide show, and click view show to view your animation. Once you have seen your animation and your slide show, right click, and select end show. This takes you back to your work area.
Step 7: The next step is to add another slide to your show On the toolbar click insert, and select new slide. Once you select new slide, go to slide layout (right side of screen), select the title and 2text layout. Go to the first box, type Workshops, now go to the second box and type Computer Repair and hit enter , next type "Electronic Communication and hit enter,
Dept. of Computer Science & Engineering, MSRIT Page "
next type Internet Based Research and hit enter, next type Navigating the Internet and hit enter, next type Introduction to PowerPoint .
Step 8: Once you have finished entering information for your first slide. You are ready to add animation to your text, or picture. Step 9: Click the slide show tab on the toolbar, and select animation schemes. Go to the right of your screen, push the down arrow in the box apply to selected slides. Select fade in one by one. Next go to slide show, and click view show to view your animation. After the first slide appears select the page down button to get to the next slide, Continue to push the page down button until you get to the end of the second slide. Right click, and select end show. This takes you back to your work area.
Step 10: Once you have done this, go to slide show on your toolbar and select view show!
Dept. of Computer Science & Engineering, MSRIT Page #$
printf("\nEnter + to add\n - to subtract\n * to multiply\n / to divide"); scanf("%c",&opcode);
switch(opcode) { case '+': result = a+b; printf("\nSum of %f & %f is : %f", a,b,result); break; case '-': result = a-b; printf("\nSubtraction of %f & %f is : %f", a,b,result); break; case '*': result = a*b; printf("\nMultiplication of %f & %f is : %f", a,b,result); break; case '/': if(b==0) { printf("Divide By Zero Error\n"); exit(0); } else { result = a/b; printf("\nDivision of %f & %f is : %f", a,b,result); } break; default:
Dept. of Computer Science & Engineering, MSRIT Page 25
printf("\nInvalid Operator");
}
getch(); }
Output: Enter 2 numbers 8 10 Enter + to add - to subtract * to multiply / to divide
* Multiplication of 8 & 10 is : 80
c) Write a C program to find roots of a quadratic equation using switch.
Dept. of Computer Science & Engineering, MSRIT Page 26
{ case 1: printf("Roots are Equal\n"); root1 = (-b)/(2*a); printf("\nRoot : %f", root1); break; case 0: printf("\nRoots are Imaginary"); break; default: printf("\nInvalid Value"); } break; } default: printf("\nInvalid Choice");
}
getch(); }
Output: Enter the values of a, b & c 4 8 3 Roots are Complex Root1: -7.50000 Root2: -8.50000
d) Write a C program to find area of a triangle/square/circle/rectangle using switch statement.
#include<stdio.h> #include<conio.h> #define PI 3.147
void main() { float radius = 0, length = 0, breadth = 0; float base = 0, height = 0, area = 0; int choice = 0; clrscr();
Dept. of Computer Science & Engineering, MSRIT Page 2
printf("Enter \n1 to find area of triangle\n2 to find area of Square"); printf("\n3 to find area of circle\n4 to find area of rectangle\n"); scanf("%d",&choice);
switch(choice) { case 1: printf("\nEnter base & height of a triangle"); scanf("%f%f",&base,&height); area = (1.0/2)*base*height; printf("\nArea of Triangle : %f", area); break; case 2: printf("\nEnter length of a Square"); scanf("%f",&length); area = length * length; printf("\nArea of Square : %f", area); break; case 3: printf("\nEnter the radius of a Circle"); scanf("%f",&radius); area = PI * radius * radius; printf("\nArea of Circle : %f", area); break; case 4: printf("\nEnter the length & breadth of a Rectangle"); scanf("%f%f",&length,&breadth); area = length * breadth; printf("\nArea of Rectangle : %f", area); break; default: printf("\nInvalid Choice");
}
getch(); }
Output: Enter 1 to find area of triangle 2 to find area of Square 3 to find area of circle 4 to find area of rectangle 2 Enter length of a square 5 Area of Square : 25
Dept. of Computer Science & Engineering, MSRIT Page 2!
Lab Exercise: 5
a) Write a C program to demonstrate use of Size of operator.
#include<stdio.h> #include<conio.h>
void main() { int a; float b; char c; double d; long int l; clrscr();
printf("Size of Intereger Variable is : %d\n", sizeof(a)); printf("Size of Float Variable is : %d\n", sizeof(b)); printf("Size of Char Variable is : %d\n", sizeof(c)); printf("Size of Double Variable is : %d\n", sizeof(d)); printf("Size of Long Integer Variable is : %d\n",sizeof(l)); getch(); }
Output: Size of Intereger Variable is : 2 Size of Float Variable is : 4 Size of Char Variable is : 1 Size of Double Variable is : 8 Size of Long Intereger Variable is : 4
b) Write a C program to find whether a entered character is a number or digit using both ASCII values and built-in functions.
/* Program without using Built-In Functions */ #include<stdio.h> #include<conio.h>
void main() { char ch; clrscr();
Dept. of Computer Science & Engineering, MSRIT Page 2"
printf("Enter a character\n"); scanf("%c",&ch); if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')) printf("\n%c is a character",ch); else if(ch>=48 && ch<=56) printf("\n%c is a Number",ch); else printf("\nInvalid Character");
getch(); }
/* Program using Built-In Functions */ #include<stdio.h> #include<conio.h>
void main() { char ch; clrscr();
printf("Enter a character\n"); scanf("%c",&ch); if(isalpha(ch) > 0) printf("\n%c is a character",ch); else if(isdigit(ch) > 0) printf("\n%c is a Number",ch); else printf("\nInvalid Character");
getch(); }
Output: Enter a character B B is a character
Dept. of Computer Science & Engineering, MSRIT Page 3$
c) Write a C program to convert a lower case alphabet to uppercase and vice-versa using both ASCII values and built-in functions.
/* Program without using Built-In Functions */ #include<stdio.h> #include<ctype.h> #include<conio.h>
if(flag == 1) printf("\n%d is found at position %d",key,mid+1); else printf("\nElement not found");
getch(); }
Output: Enter the size of the array 6 Enter 6 elements into Array 3 8 4 6 2 1 Array Elements are : 3 8 4 6 2 1 Enter the element to search in the array 2
2 is found at position 5
Dept. of Computer Science & Engineering, MSRIT Page 45
Lab Exercise: 8
a) Write a program to find length of string without using built-in functions.
#include<stdio.h> #include<conio.h>
void main() { char str[20], c; int i; clrscr();
printf("\nEnter the String"); scanf("%s",str); printf("\n\nEntered string is %s\n",str); for(i=0;c!='\0';++i) { c=str[i]; } printf("\nLength of the entered string is : %d",i-1);
getch(); }
Output: Enter the String MSRIT Entered string is MSRIT Length of the entered string is : 5
b) Write a program to copy from one string to another without using built-in functions.
#include<stdio.h> #include<conio.h>
void main() { char str[20], new_str[20]; int i; clrscr();
printf("\nEnter the String");
Dept. of Computer Science & Engineering, MSRIT Page 46
scanf("%s",str); printf("\n\nEntered string is %s\n",str); for(i=0;str[i]!='\0';++i) { new_str[i]=str[i]; } new_str[i]='\0'; printf("\nCopied string is : %s",new_str);
getch(); }
Output: Enter a string QWERTY Entered string is QWERTY
Copied string is : QWERTY
c) Write a program to combine two strings without using built-in functions.
strcpy(str1,str); printf("\nCopied string is : %s\n",str1);
printf("\nEnter a string"); scanf("%s",str2);
if((strcmp(str,str2))==0) printf("\nStrings are equal\n"); else printf("\nStrings are not equal\n");
i = strlen(str); printf("\nLength of string %s is : %d\n",str,i);
printf("\nConcatenated String is : %s\n",strcat(str,str2)); getch(); }
Output: Enter a string CSE Copied string is : CSE Enter a string ISE Strings are not equal Length of the string CSE is : 3 Concatenated String is : CSEISE
Dept. of Computer Science & Engineering, MSRIT Page 52
d) Write a C program to add two matrices.
#include<stdio.h> #include<conio.h>
void main() { int a[10][10], b[10][10],c[10][10]; int i=0, j=0; int rows=0, cols=0; int rows1=0, cols1=0; clrscr();
printf("Enter the number of rows & columns for the first matrix\n"); scanf("%d%d", &rows,&cols); printf("\nEnter the elements for the First Matrix:\n"); for(i=0;i<rows;i++) { for(j=0;j<cols;j++) { scanf("%d",&a[i][j]); } }
printf("\nEnter the number of rows & columns for the second matrix\n"); scanf("%d%d",&rows1,&cols1); printf("\nEnter the elements for the Second Matrix:\n"); for(i=0;i<rows1;i++) { for(j=0;j<cols1;j++) { scanf("%d",&b[i][j]); } }
if(rows != rows1 || cols != cols1) { printf("\nAddition can not be performed\n"); exit(0); }
for(i=0;i<rows;i++) { for(j=0;j<cols;j++) {
Dept. of Computer Science & Engineering, MSRIT Page 53
Output: Enter the number of rows & columns for the first matrix 3 3 Enter the elements of the First Matrix 1 2 3 4 5 6 7 8 9 Enter the number of rows & columns for the second matrix 3 3 Enter the elements of the Second Matrix 9 8 7 6 5 4 3 2 1
Resultant Matrix: 10 10 10 10 10 10 10 10 10
Dept. of Computer Science & Engineering, MSRIT Page 54
Lab Exercise: 10
a) Write a C program to find trace and norm of a given matrix.
void main() { int a[10][10]; int i=0, j=0; int rows=0, cols=0; int trace=0, norm=0; clrscr();
printf("Enter the number of rows & columns for the first matrix\n"); scanf("%d%d", &rows,&cols); printf("\nEnter the elements for the Matrix:\n"); for(i=0;i<rows;i++) { for(j=0;j<cols;j++) { scanf("%d",&a[i][j]); } }
Dept. of Computer Science & Engineering, MSRIT Page 55
} }
printf("\nTrace of the Entered Matrix : %d", trace); printf("\nNorm of the Entered Matrix : %d", norm); getch(); }
Output:
Enter the number of rows & columns for the first matrix 3 3 Enter the elements for the Matrix 1 2 3 4 5 6 7 8 9 Matrix is : 1 2 3 4 5 6 7 8 9 Trace of the Entered Matrix : 15 Norm of the Entered Matrix : 285
b) Write a C program to multiply two matrices.
#include<stdio.h> #include<conio.h>
void main() { int m1[10][10],m2[10][10],mult[10][10]; int r1,c1,r2,c2; int i=0, j=0, k=0; clrscr();
printf("Enter number of rows and columns of First Matrix\n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of Second Matrix\n"); scanf("%d%d",&r2,&c2); if(r2==c1) { printf("Enter the elements for the First matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++)
Dept. of Computer Science & Engineering, MSRIT Page 56
scanf("%d",&m1[i][j]); } printf("First Matrix :\n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",m1[i][j]); printf("\n"); } printf("Enter the elements for the Second matrix \n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); } printf("Second Matrix :\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",m2[i][j]); printf("\n"); }
Dept. of Computer Science & Engineering, MSRIT Page 5
} else { printf("Matrix multiplication cannot be done"); } }
Output: Enter number of rows and columns of First Matrix 2 2 Enter number of rows and columns of Second Matrix 2 2 Enter the elements for the First matrix 2 3 4 5 Enter the elements for the First matrix 5 4 3 2 Resultant Matrix: 19 14 35 26
c) Write a C program to sort elements in ascending order using bubble sort with functions.
#include<stdio.h> #include<conio.h>
void main() { int size=0,arr[10]; int i=0, j=0, temp=0; clrscr();
printf("\nEnter the size of the array\n"); scanf("%d",&size);
printf("\nEnter the %d elements into the array\n",size); for(i=0;i<size;i++) { scanf("%d",&arr[i]); } for(i=0;i<size;i++) { for(j=i+1;j<size;j++) {
Dept. of Computer Science & Engineering, MSRIT Page 5!
Output: Enter the size of the array 6 Enter the 6 elements into the array 9 2 8 4 6 1 Sorted Array: 1 2 4 6 8 9
Dept. of Computer Science & Engineering, MSRIT Page 5"
Lab Exercise: 11
a) Write a C program to multiply two matrices using functions.
#include<stdio.h> #include<conio.h>
void multiply_matrix(int [10][10], int [10][10], int [10][10], int, int);
void main() { int m1[10][10],m2[10][10],m3[10][10]; int r1,c1,r2,c2; int i=0, j=0; clrscr();
printf("Enter number of rows and columns of First Matrix\n"); scanf("%d%d",&r1,&c1); printf("Enter number of rows and columns of Second Matrix\n"); scanf("%d%d",&r2,&c2); if(r2==c1) { printf("Enter the elements for the First Matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&m1[i][j]); } printf("First Matrix : \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",m1[i][j]); printf("\n"); } printf("Enter the elements for the Second Matrix \n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&m2[i][j]); } printf("Second Matrix :\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",m2[i][j]);
Dept. of Computer Science & Engineering, MSRIT Page 6$
void multiply_matrix(int m1[10][10], int m2[10][10],int mult[10][10], int row, int col) { int i, j, k; for(i=0;i<row;i++) { for(j=0;j<col;j++) { mult[i][j]=0; for(k=0;k<row;k++) { mult[i][j]+=m1[i][k]*m2[k][j]; } //printf("%d\t",mult[i][j]); } } printf("\nResultant Matrix :\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%d\t",mult[i][j]); } printf("\n"); } }
Output: Enter number of rows and columns of First Matrix 2 2 Enter number of rows and columns of Second Matrix 2 2 Enter the elements for the First matrix
Dept. of Computer Science & Engineering, MSRIT Page 6#
2 3 4 5 Enter the elements for the First matrix 5 4 3 2 Resultant Matrix: 19 14 35 26
b) Write a program to determine the length of a character string using pointers.
#include<stdio.h> #include<conio.h>
void main() { int length=0; char *p; char str[20]; clrscr();
printf("\nEnter a string\n"); scanf("%s", str);
p = str;
while((*p)!='\0') { length++; p++; }
printf("\nLength of the %s is : %d", str,length); getch(); }
Output:
Enter a string MSRIT Length of the string MSRIT is : 5
Dept. of Computer Science & Engineering, MSRIT Page 62
c) Write a program to input N student information such as name, id, percentage. Display the information of the student with the highest percentage using array of structures.
#include<stdio.h> #include<conio.h>
void main() { struct student { char name[20]; int id; int perc; }stud[10];
int i=0, n=0, index=0; int high; clrscr();
printf("\nEnter number of records to read\n"); scanf("%d",&n);
printf("\nN: %d\n",n); for(i=1;i<=n;i++) { printf("\nEnter the name, id and percentage of %d student",i); scanf("%s",stud[i].name); scanf("%d",&stud[i].id); scanf("%d",&stud[i].perc); }
high = stud[0].perc; index = 0; for(i=0; i<n; i++) { if(high<stud[i].perc) { index = i; high = stud[i].perc; } } printf(\nStudent with highest percentage is :\n); printf("\n\n%s\t%d\t%d\n",stud[index].name, stud[index].id, stud[index].perc); getch(); }
Dept. of Computer Science & Engineering, MSRIT Page 63
Output:
Enter the number of records to read 4 Enter the name, id and percentage of 1 student ABC 100 88 Enter the name, id and percentage of 2 student MNO 101 90 Enter the name, id and percentage of 3 student STU 102 91 Enter the name, id and percentage of 4 student XYZ 103 89
Student with highest percentage is : STU 102 91
Dept. of Computer Science & Engineering, MSRIT Page 64
Lab Exercise: 12
a) Write a program to illustrate use of pointers in expressions.
#include<stdio.h> #include<conio.h>
void main() { int *p, *q; int m, n; int res, res1; clrscr();
printf("\nEnter two numbers"); scanf("%d%d",&m,&n);