Você está na página 1de 31

QUESTION AND ANSWERS Sub: :- COMPUTER CONCEPTS AND C PROGRAMMING Unit-3 Q1. What is an array?

Ans:- An array is a collection of variables of the same type. Individual array elements are identified by an integer index. In C the index begins at zero and is always written inside square brackets. Q2. How arrays are declared? Ans:- ARRAY DECLARATION 1. ONE DIMENSIONAL ARRAY data type variable name[ size] Ex: int a[10]; char name [10] ; float x [5]; 2. TWO DIMENSIONAL ARRAY data type variable [size][size]; Ex: int mat_a[10][10]; char name[10][20]; Arrays can have more dimensions, in which case they might be declared as int results_2d[20][5]; int results_3d[20][5][3]; Each index has its own set of square brackets. Q3. What is searching? Explain the method of linear searching. Ans:- Searching:-It is a process of finding a key element in a given array of elements. If the key element is found in the array of elements then the search is said to be successful otherwise unsuccessful. 1.Linear searching.:- In this method the key element is compared with the elements of the array one by one linearly till the key element is found in the array or the search area is exhausted. #include<stdio.h> main() { float a[10],x; int i,n,flag=0; printf("\n enter the no.of elements in the array "); scanf("%d",&n); printf("\n enter the elements of the array\n"); for(i=0;i<n;i++) scanf("%f",&a[i]); printf("\n enter the key element to be searched "); scanf("%f",&x); for(i=0;i<n;i++)

if(a[i]==x) { printf("\n the key element %f is found at location %d",x,i+1); flag=1; } if(flag==0) printf("\n the key element %f is not found",x); getch(); } Q4. What is binary searching? Write a C code for binary searching. Ans:- This method is more efficient than linear search, when size of array is large. This method of searching is performed by following steps: 1. arrange the array in the ascending order. 2. read the key element to be searched 3. compare the key element with the mid element of the array Now there are 3 possibilities a. if the key element is equal to mid element then the searching is declared as successful. b. If the key element is greater than the mid element, then conduct the search in 2nd half of the array. c. If the key element is less than the mid value then conduct the search in the 1st half of the array. Binary searching:#include<stdio.h> main() { int i,n,mid,low=0,high,probe=0; float a[10],temp,x; printf("\n enter the no.of elements to be sorted "); scanf("%d",&n); printf("\n enter %d elements in ascending order\n",n); for(i=0;i<n;i++) scanf("%f",&a[i]); /*binary search*/ printf("\n enter the element to be searched "); scanf("%f",&x); high=n-1; while(low<=high) { mid=(low+high)/2; if(x<a[mid]) {

high=mid-1; probe++; } else if(x>a[mid]) { low=mid+1; probe++; } else if(x==a[mid]) { probe++; printf("\n search is successful"); printf("\n the no.of probes=%d",probe); getch(); exit(); } } printf("\n unsuccessful search"); getch(); }

Q5. Ans:-

Write a C program to sort N numbers in ascending order using bubble sort ? #include<stdio.h> main() { int i,j,n; float a[10],temp; printf("\n enter the no.of elements to be sorted "); scanf("%d",&n); printf("\n enter %d elements \n",n); for(i=0;i<n;i++) scanf("%f",&a[i]); printf("\n The entered array is\n"); for(i=0;i<n;i++) printf(" %f",a[i]); /*bubble sorting*/ for(i=1;i<=n-1;i++) for(j=0;j<n-i;j++) if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; /*swaping*/ a[j+1]=temp;

} printf("\n the sorted array is\n"); for(i=0;i<n;i++) printf(" %f",a[i]); getch(); } Q6. What is the necessity of user defined function ? Ans:- If the program is lengthier, then it is very difficult for the programmer to handle it. Normally, the larger programs are more prone to errors and it would be a tedious job to locate and correct these errors. Therefore, such programs should be broken down into a number of smaller logical components, each of which serve a specific task. The process of splitting the lengthier and complex programs into a number of smaller units ( called modules or subprograms) is called modularization. Programming with such an approach is called modular programming. Q7. List the advantages of user defined function ? Ans:- There are many advantages in going for modularization. Some of them are listed below. Reusability: If a particular set of instructions are to be accessed repeatedly from several different places within the program, then we can make this group of instructions as one module and call it wherever necessary. This avoids rewriting of function on every access. Debugging is easier: Since each module is smaller and clearer, the user can easily locate the errors and correct them. Build library: It allows the programmer to build a library of the most commonly used subprograms. This reduces the time and space complexity. It is also promotes the portability.

Q8. Write the different ways to classify the functions. Ans:- The functions are classified into standard functions and user-defined functions. The standard functions are also called library functions or built in functions. All standard functions, such as sqrt(), abs(), log (), sin(), pow() B etc. are provided in the library of functions. These are selective. But, the most of the applications need other functions than those available in the software. This type of functions must be written by the user (programmer), hence, the name user-defined functions. This chapter mainly deals with user defined functions. Q9. Write a note on Function definition Ans:- Defining a function means writing an actual code for the function which does a specific and identifiable task. Suppose you are defining a function which computes the square of a given number. Then you have to write a set of instructions to do this. There are different methods to define functions.

Function_type_function_name (parameter list) Parameter definition; { variable declaration ; executable statement 1; executable statement 2 . . . return statement ( value_computed); }

/* within the function */

Q10. What is formal parameter? Ans:- Formal Parameter List The parameter list declares the variables that will receive the data sent by the calling program. They serve as input data to the function to carry out the specified task. Since they represent actual input values, they are often referred to as formal parameters. These parameters can also be used to send values to the calling programs. This aspect will be covered later when we discuss more about functions. The parameters are also known as arguments.

Q11. Describe Return statement along with syntax? Ans:- As pointed out earlier, a function may or may not send back any value to the calling function. If it does, it is done through the return statement. While it is possible to pass to the called function any number of values, the called function can only return one value per call, at the most. The return statement can take one of the following form return; or return (expression); The first, the `plain return does not return any value; it acts much as the closing brace of the function. When a return is encountered, the control is immediately passed back to the calling function. An example of the use of a simple return is as follows; If (error) return; The second form of return with an expression returns the value of expession. For example the function Int mul (int x, int y) { int p; p = x*y;

return (p) ; } returns the value of p which is the product of the values of x and y. The last two statements can be combined into one statement as follows: retun (x*y); A function may have more than one return statements. This situation arises when the value returned is based on certain conditions. For example: If (x<=0) return (0); else return (1); Q12. What type of data does a function return? Ans:- All functions by default return int type data. We can force a function to return a particular type of data by using a type specifier in the function header. When a value is returned, it is automatically cast to the functions type. In functions that do computations using doubles, yet return int , the returned value will be truncated to an integer. Q13. What is a string? List any four string handling functions. Ans:- In C Strings are defined as arrays of characters. For example, the following defines a string of 50 characters: char name[50]; NOTE: We just need to give the name of the string. In order to allow variable length strings the \0 character is used to indicate the end of a string. String handling functions strcat(str1,str2) concatenates two strings strcmp(str1,str2) compares two strings strcpy(str1,str2) copies str2 to str1 strlen(str) returns length of str

Q14. Ans:i)

Explain the following String handling functions with an example. strcpy() The strcpy function works almost like a string assignment operator, the general form of strcpy is strcpy ( string1, string2); this function will assign the contents of string 2 to string 1, Ex:- strcpy ( message , input)

ii)

The contents of the string variable input will be assigned to the string variable messge. strcat() The strcat function joins two strings together, the general form is Strcat ( string1, string 2); String 1 and string2 are character arrays, when the function is executed , string2 is appended to string1, it does so by removing the null character at the end of string 1 and placing string2 from there Ex : - str1= Electrical Str2= Engineering Strcat(str1,str2); After execution the result will be Electrical Engineering \0

iii)

strlen() This function counts and return the value the number of characters in a string, Variable_name = strlen(string); The variable name is an integer variable which receives the value of the length of the string Ex:Str1= GOOD A=strlen(str1) Gives 4 as the result and assign 4 to A strcmp() strcmp function alphabetically compares two strings and returns an integer based on the outcome if string 1 is less than string2 it gives value less than zero if string 1 is equal to string2 it gives value 0 if string 1 is greater than string 2 it gives value greater than zero strcmp(string1,string2) it gives the difference of the first non matching characters ASCII value Ex :- strcmp(rama,rama); It returns value = 0;

iv)

v)

strncat() This function is used to append first n characters of the second string at the end of the first string Strncat(string1,string2,n); Ex:- strncat (system,software engg,8);

The result will be systemsoftware Q15. What is a Pointer ? Ans:- A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. Q16. Explain the operators of the pointers ? Ans: The unary or monadic operator & gives the address of a variable. The indirection or dereference operator * gives the contents of an object pointed to by a pointer.

Q17. How to declare a pointer ? Ans:- To declare a pointer to a variable do: int *pointer; NOTE: We must associate a pointer to a particular type: You can't assign the address of a short int to a long int, for instance. Consider the effect of the following code: int x = 1, y = 2; int *ip; ip = &x; y = *ip; x = ip; *ip = 3; It is worth considering what is going on at the machine level in memory to fully understand how pointer work. Consider Fig(next slide). Assume for the sake of this discussion that variable x resides at memory location 100, y at 200 and ip at 1000. Note A pointer is a variable and thus its values need to be stored somewhere. It is the nature of the pointers value that is new. IMPORTANT: When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. So ... int *ip; *ip = 100; will generate an error (program crash!!). The correct use is: int *ip; int x; ip = &x; *ip = 100;

Q18. What are the different storage classes in C? Ans:- Storage class (Scope & lifetime of a variable)

1) 2) 3) 4)

Based on where the variables are declared, the scope & lifetime of variables change. Global or External variables Local or automatic variables Static variables. Register variable.

Q19. what is external variable explain with an example? Ans:- Global/External variables: These variables are usually defined before all functions. Memory is allocated for these variables only once & are initialized to zero. These variables can be accessed by any function and are alive & active throughout the programs. Any function can use it & change its value. Memory is destroyed once the execution is over. Ex: # include (stdio.h) Int a=10; Int b=20; Int c; Void add ( ) { c=a+b; } void subtract ( ) { c=a-b; } main ( ) { add ( ); printf (a=%d & b=%d sum=%d, a,b,c); subtract); printf (a=%d b=%d diff=%d, a,b,c); } Q20. what is automatic variable explain with an example? Ans:Automatic variables are defined at the beginning of a function in which they are used. Where function is called, memory is allocated to these variables & as the control comes out the memory is deallocated. Therefore these ar3 local/private to the function is which they are defined. They are also called Internal variable. Scope of these are limited I cannot be accessed outside

Ex:

void add (int a, int b) { int c; c=a+b; print f (Result=%d\n, c); } main ( ) { int a=10; int b=20; } print f (a=%d\n b=%d\n, a,b); } C is local variable to function add, after the cofntrol goes out of function C cannot be accessed. If we use C outside, it has to be redefined, Otherwise an eror undefined symbol C is displayed by the compiler. A&b are also local variables. Q21. what is static variable explain with an example? Ans:- Unlike as automatic variable, memory allocated for static variable will not be destroyed as the control goes out of the function. So, the current value of the static variables is available where the function is called next time. Note: Static variables like automatic variables cannot be accessed by any other function except the function in which they are defined. So it has some char of local variable & some characteristic of a global variable. # include <stdio.h> void display ( ) { static int i=o; i++; print f (%d\n,i); } main ( ) { int j; for (j=0, j<5; j++) display ( ); { void display ( ) { static int x=o; X++ Print f (the values of x in function is % d\n,x); } main ( ) {

int x; class ( ); for (x=; x<5; x++) { print f (the value of x in for is %d\n,x); display ( ) ; } } # o/p is The value of x in for is The value of x in function is 1 The value of x in for is The value of x in function is 2 The value of x in for is The value of x in function is 3 The value of x in for is The value of x in function is 4 The value of x in for is The value of x in function is 5 w/o-static 0 1 1 1 2 1 3 1 4 1

(And there will be no clash in x local variable in main & function) O/P 1 here I is defined as a static variable 2 * I cannot be accessed outside this function. 3 * Memory allocated for this static variable i will not be destroyed 4 as control goes out of the function. 5 * current value of static variable is available when a function is called next time Q22. what is Register variable explain with an example? Ans:- Any variable declared with the qualifier register is called a register variable. This declaration advises the compiler that the variable under use is to be stored in one of the registers. Register accessing is much faster compared to the memory access & hence, frequently accessed variable such as loop variables are usually stored in register variables. This leader to faster execution of the program. If the register is not free, the compiler can ignore this & the memory is allocated as is dare for other variables Ex: register int x; This is done \ allowed only for local (automatic) variable & to the formal parameters.

Q23. Explain the construction and working of a laser printer. Ans:- LASER PRINTER:- Photo copying technology has been used in laser printers, A drum coated with positively charged photo conductive material is scanned by a laser beam. The positive charges that are illuminated by the beam are dissipated, then a negatively charged toner powder is spread over the drum. It adheres to the positive charges, thus creating a page image that is then transferred to the paper. As the last step the drum is cleaned of any excess toner material to prepare it for printing the next page. Laser printers offer better quality and speed , Text and graphics can be combined and high print speeds like 20,000 lines per minute can be achieved, Its price is high. Q24. List the different input devices. Ans:- Input devices are KEYBOARD, MOUSE, LIGHTPEN, JOYSTICK , SCANNER , DIGITIZER, OPTICAL SCANNERS ETC,.

Q25. Explain the features of the keyboard Ans:- KEYBOARD: The keyboard is the most friendly input peripheral, both data and program can be keyed in through it, In addition , certain commands to software can be given from the keyboard, it is almost impossible to use a computer without a keyboard. Keyboard consists of a set of key switches. There is one keyswithch for each letter, number, symbol etc., much like a typewriter, when a key is pressed a keyswitch is activated, there are two types of keyboards, I) serial keyboard, sends the data bit by bit and ii) a parallel keyboard, sends the data as a byte in parallel form, all the bits are sent simultaneously on different lines. The function of the keyboard electronics are , sensing a key depression, encoding and sending the code to computer,. Ordinary keyboard will have 1 Functional keys (F1 . . . . F 12) 2 Print screen, Scroll lock, Pause/Break 3 Num/Caps? Scroll Lock (LEDs) 4 Escape key 5 Numeric Keypad ( 0 9,+ , - , Enter, Insert, Delete ) 6 0 9 , A Z Caps lock, Tab , Shift , Ctrl, Alt, Enter , Backspace, Special keys like $, %, #, & ( , ) , , ., : etc. 7 Arrow keys 8 Editing Keys ( Page up, Page down , Home , End, Insert, delete)

Q26. Write a brief note on floppy disk. Ans:- FLOPPY DISK: 1. It consists of circular magnetic disk, made from thins flexible mylar plastic sheet coated with magnetic oxide. This disk is closed in antistatic cover for protection dust. 2. The diskette that is prepared for storing the information consists of 2 sides, top and bottom. Each side consists of number of tracks and each track is divided into number of sectors, each stores number of bytes of information. 1 byte is required to store one character, say alphabetic or number. Data and programs are stored in a disk as files. A filename is nothing but a collection of related data. The filenames of different files stored in a diskette are stored in one area of disk, called as DIRECTORY. Track s Sector of track

3.

4.

Based on the amount of data it can store, floppy disks are available in following capacities. a. 360 KB (360X1024 bytes(, where K=1024 (2 sides X 40 tracks X 9 sectors X 512 bytes = 360 Kbytes) b. 1.2 Mb (1.2 X 1024 X 1024) (2 sides X 80 tracks x 18 sectors X 512 bytes = 1.2Mbytes) c. 1.44 MB (1.44 X 1024 X 1024) (2 sides X 80 tracks X 18 sectors X 512 bytes = 1.44Mbytes) Structure of floppy disk: Write Protect notch Spindle Hole Index Hole Read/Write notch

Company Label

User Label

a. Write Protect Notch: when this notch is closed information cannot be written onto floppy, but reading is possible. When it is open, both reading and writing is allowed. b. Index Hole: Index hole is used to identify the starting sector of a track. c. Drive Hub Rings: Sits on drive hub, so that diskette rotates whenever motor in the drive rotates. d. Read/Write Slot: This is used for the movement of Read/Write head over the disk surface. e. Manufacture Label: Shows diskette manufacturer name, size of diskettes. f. User Label: is the space provided for the user to write any name, which indicates the contents of the diskette. Computers have one/two, floppy diskette drive, called FDD, used to read and write floppy diskettes.

Q27. Write a brief note on HARD DISK. Ans:- HARD DISK Platter
Spindle

Read/ write head

Hard disk consists of more than one circular magnetic disk, called platters. Since it consists of more than one disk more information can be stored in Hard disk compared to floppy disk. Each disk is made up to Aluminum material with a number of tracks coated with magnetic oxide. Since it is made up of aluminum, it is called Hard disk. Data can be stored on both sides of the surface. Hence two Read/Write heads are provided for each disk. Tracks and sectors are formed on each side of the disk, similar to floppy disk. Hard disk and associated electronic circuits for read and write forms an hardware unit called as Hard disk drive (HDD). HDD is enclosed in hermetically sealed enclosure, as it is very sensitive to dust. It is permanently inside the system unit, unlike floppy diskettes, which can be taken from one system to another. Hard disks are faster and provide larger storage capacity, compared to floppy disks. Of course, hard disks are costlier. Hard disks are available in different storage capacities like 1.2 GB, 3.2GB, 4.3GB Etc.,

Q28. Explain in brief operation of LAN Ans:- LAN (Local Area Network) LAN refers to many interconnected computers located generally in the same building; LAN has generally 3 characteristic features, A diameter of not more than a few Kilometers A total data rate of at least several Mbps. Complete ownership by a single organization, There are various ways for interconnecting computers such as star, common bus and ring type LAN and so on, LAN technologies provide the highest speed connections among computers, but sacrifice the ability to span large distances. Ex: Departmental computers on a campus, Q29. Explain in brief operation of WAN Ans:- WAN (Wide area network) WAN , sometimes called long haul networks, allow end pints to be arbitrarily far apart and are intended for use over large distance. Typically, WANs span entire countries have data rates below 1 Mbps and are owned by multiple organizations, they can be connected through public or private communication systems. Q30. What is E-mail explain? Ans:- E-mail (Electronic mail): Allows a user to compose memos and send them to the individual or groups. E-mail also provides the facility of reading memos that they have received. E-mail is one of the most popular and widespread Internet application services. E-mail uses the client-server paradigm. They use TCP/IP protocol, TCP/IP mail delivery system operates by having the senders machine contact the receivers machine directly.

---------------xxxxxxxxxxxxxx-----------------------

EXAMINATION QUESTIONS AND ANSWERS


Q1. Write C-program to read N integer numbers interactively and to print biggest and smallest numbers. Ans: - #include<stdio.h> main ( ) { int a[20], big, small, I, N printf(\n enter the member of integer nos); scanf(%d, &N); printf(\n enter the elements of array); for (i=1; i<=n; i++) scanf(%d, a[i]); small=big=a[1]; for (i=2; i<=n; i++) { if (big <a[i]) big=a[i]; if (small>a[i]) small = a[i]; } printf(the largest = %d, smallest = %d, big, small); } Q2. Write a function to test whether or not a given integer number is prime . Write main() which reads the integer to be tested from key board and calls the function to test for prime ness ? (10) Ans:#include<stdio.h> main() { int no,res; int prime (int); clrscr(); printf("\n enter the no \n"); scanf("%d",&no); res=prime(no); if(res==0) printf("\n the entered no is prime\n"); else printf("\n the no is not a prime no \n"); getch(); }

int prime(n) { int i,p=0; for(i=2;i<=n/2;i++) if (n%i==0) { p=1; break; } return p; Q3 } Write a function which reverses a list of N integers stored in an array passed to it . The main should read N integers into an array and print the reversed list? (10) #include<stdio.h> main() { int no,n,a[10],i; void reverse(int *ptr, int ); clrscr(); printf("enter the no of elements in the array\n"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("the entered nos are \n"); for(i=0;i<n;i++) printf(" %d",a[i]); reverse(a,n); } void reverse(int *b,int no) { int rev,digit,i; printf("\n the reversed elements of the array are \n"); for(i=0;i<no;i++) { rev=0; while(b[i]) { digit=b[i]%10; rev=rev*10+digit; b[i]=b[i]/10; } printf(" %d",rev); } }

Ans:-

Q4. Write a C program to find the standard deviation of n values Ans :#include<stdio.h> #include<math.h> main() { int i,n; float a[10],mean,sd,var,sum1=0,sum2=0; printf("\n Enter the no.of elements in the array "); scanf("%d",&n); printf("\n Enter the elements of the array \n"); for(i=0;i<n;i++) { scanf("%f",&a[i]); sum1+=a[i]; } mean=sum1/n; printf("\n Mean=%.3f",mean); for(i=0;i<n;i++) sum2+=pow((a[i]-mean),2); var=sum2/n; printf("\n Variance=%.3f",var); sd=sqrt(var); printf("\n Standard deviation=%.3f",sd); getch(); } Q5. Write a C program to Find the trace of the resultant matrix if it exists. Ans:#include<stdio.h> main() { float a[10][10],trace,x; int m,n,i,j; printf("\n Enter the order of the matrix "); scanf("%d%d",&m,&n); printf("\n Enter the elements of matrix a \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%f",&a[I][j]); } if(m==n)

{ trace=0; for(i=0;i<m;i++) trace+=c[i][i]; printf("\n Trace =%f",trace); } else printf("\n Trace does not exist"); getch(); } Q6. Explain any five string handling functions in C with appropriate examples (10) Ans:String handling functions vi) strcpy() The strcpy function works almost like a string assignment operator, the general form of strcpy is strcpy ( string1, string2); this function will assign the contents of string 2 to string 1, Ex:- strcpy ( message , input) The contents of the string variable input will be assigned to the string variable messge. strcat() The strcat function joins two strings together, the general form is Strcat ( string1, string 2); String 1 and string2 are character arrays, when the function is executed , string2 is appended to string1, it does so by removing the null character at the end of string 1 and placing string2 from there Ex : - str1= computer Str2= programming Strcat(str1,str2); After execution the result will be computer programming\0 viii) strlen() This function counts and return the value the number of characters in a string, Variable_name = strlen(string); The variable name is an integer variable which receives the value of the length of the string

vii)

Ex:Str1= GOOD A=strlen(str1) Gives 4 as the result and assign 4 to A strcmp() strcmp function alphabetically compares two strings and returns an integer based on the outcome if string 1 is less than string2 it gives value less than zero if string 1 is equal to string2 it gives value 0 if string 1 is greater than string 2 it gives value greater than zero strcmp(string1,string2) it gives the difference of the first non matching characters ASCII value Ex :- strcmp(rama,rama); It returns value = 0; x) strncat() This function is used to append first n characters of the second string at the end of the first string Strncat(string1,string2,n); Ex:- strncat (system,software engg,8); The result will be systemsoftware

ix)

Q7. Explain the difference between call by value and call by reference with suitable examples (5marks) Ans:- Call by value: This method copies the value of an argument into the formal parameter of the function, therefore changes made to the parameters of the function have no effect on the variables used to call it Ex:# include<stdio.h> Void main() { int t=10; print (%d %d,sqr(t),t); } sqr(int x) { x=x*x; return(x);

} in the above example the value of the argument of sqr ( ), 10 is copied into the parameter x, when the assignment x= x*x takes place only the local variable x is modified, the variable t used to call sqr ( ) still has the value 10. Hence the output is 100 10. Call by reference: In this method the address of an argument is copied into the parameter. Inside the function the address is used to access the actual argument used in the call. This means the changes made to the parameter affect the variable used to call the routine. We can create a call by reference by passing a pointer to the argument. The parameter has to be declared as pointer type. Ex:main( ) { int x,y; x=10; y=10; swap ( &x , &y ) } void swap ( int *x, int *y) { int temp; temp = *x; x = *y; *y = temp; } In this the contents of the variables used to call the function are swapped. Q8. Write a function using pointers to determine the length of a character string (5marks) Ans:- #include<stdio.h> main() { char a[10]; void fun(char *ptr); printf(" \n enter the string "); gets(a); fun(a); } void fun( char *b) { int i=0; while(b[i]!='\0') i++;

printf("the length of the string is %d",i); } Q 9. Write a short notes on (i) Scope and lifetime of a variable (ii) Pointers in C Ans:i) Scope and lifetime of a variable:

(2*5)

A variable in C can have any one of the four storage class 1. Automatic variables Which are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited. So they are termed as local or internal variables 2. External Variables Variables that are both alive and active throughout the entire program are known as external variables. They are also called as global variables. Which can be accessed by any function in the program. These are declared outside the function. 3. Static variables The value of static variables persists until the end of the program., we can have internal type of an external type, depending where we have defined.

4.

Register variables This tells the compiler that the variable should be kept in one of the machines registers , instead of in the memory, variable s in the register the execution time is faster The life of this is until the end of function or block. Pointers in C The pointer is a variable that represents the location of a data item, such as a variable or an array element, pointers are used because they reduce the length and complexity of the program and also increase the execution speed. In particular pointers also provide a way to return multiple data items from a function via function arguments. Pointers provide a convenient way to represent multidimensional arrays, Ex:- if int price = 50; &price gives the address of price if ptr is a pointer

ii)

ptr=&price is a valid statement. Q10. Write a C program to read a sentence and count the number of vowels and consonants in the given sentence. Output the results on two lines with suitable headings (10marks) Ans:#include<stdio.h> #include<ctype.h> main() { int i=0,vowel=0,cons=0; char str[50]; printf("\n Enter a sentence "); gets(str); while(str[i]!='\0') { if(isalpha(str[i])) switch(str[i]) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u':vowel++; break; default :cons++; } i++; } printf("\n The entered sentence is -"); puts(str); printf("\n The no.of vowels =%d",vowel); printf("\n The no.of consonants =%d",cons); getch(); } Q11. Write a C program add or subtract two matrices A (M x N) and B (M x N) Output the given matrices, their sum OR difference (10marks) Ans:-

#include<stdio.h> main() { float a[10][10],b[10][10],c[10][10], x; int m,n,i,j; char op; printf("\n Enter the order of the matrix "); scanf("%d%d",&m,&n); printf("\n Enter the elements of matrix a \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%f",&x); a[i][j]=x; } printf("\n Enter the elements of matrix b \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%f",&x); b[i][j]=x; } printf("\n Enter + for addition, - for subtraction "); scanf("\n%c",&op); switch(op) { case '+': for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; break; case '-':for(i=0;i<m;i++) for(j=0;j<n;j++) c[i][j]=a[i][j]-b[i][j]; break; default :printf("\n wrong operator "); exit(0); } printf("\n The given matrices are"); printf("\n The matrix a is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%f ",a[i][j]); printf("\n"); } printf("\n The matrix b is\n");

for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%f ",b[i][j]); printf("\n"); } printf("\n The value of matrix a %c matrix b is \n",op); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%f ",c[i][j]); printf("\n"); } getch(); } Q12. Write a C program to read a matrix A (M x N) and find the norm of the matrix . Print the Output ? (10marks) Ans:#include<stdio.h> main() { float a[10][10],norm ,x; int m,n,i,j; printf("\n Enter the order of the matrix "); scanf("%d%d",&m,&n); printf("\n Enter the elements of matrix a \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%f",&a[i][j]); } printf(\n The matrix a is\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%f ",a[i][j]); printf("\n"); } norm=0; for(i=0;i<m;i++) for(j=0;j<n;j++)

norm+=a[i][j]*a[i][j]; printf("\n NORM=%f",norm); getch(); } Q13. write aC program to read a string and convert lower case to upper case and vice-versa (8marks) Ans:#include<stdio.h> #include<ctype.h> #include<conio.h> main() { int i=0; char ch[20]; printf("\n enter a string "); gets(ch); printf(\n The entered string is ); puts(ch); for(i=0;ch[i]!='\0';i++) { if(ch[i]>='a'&&ch[i]<='z') ch[i]=toupper(ch[i]); else ch[i]=tolower(ch[i]); } printf("\n converted string is\n"); puts(ch); getch(); } Q14. Write a C program to find the length of a string (without using library functions). (8marks) Ans:#include<stdio.h> main() { int i=0,length; char str1[50]; printf("\n Enter the string "); gets(str1); printf("\ The first string is - "); puts(str1);

while(str1[i]!='\0') i++; length=i; prinyf( the length = %d, length); getch(); } Q15. Write a C program to read two strings and concatenate them (without using library functions). Output the concatenated string along with the given strings. (10marks) Ans:#include<stdio.h> main() { int i=0,length1; char str1[50],str2[25]; printf("\n Enter the first string "); gets(str1); printf("\n Enter the second string "); gets(str2); printf("\ The first string is - "); puts(str1); printf("\n The second string is - "); puts(str2); while(str1[i]!='\0') i++; length1=i; i=0; while(str2[i]!='\0') { str1[length1+i]=str2[i]; i++; } str1[length1+i]='\0'; printf("\n The concatenated string is - "); puts(str1); getch(); } Q16. Develop functions a. To read a given matrix. b. To output a matrix c. To computer the product of two matrices.

Use the above developed functions to read in two matrices A(M x N) and B(N x M), to compute the product of the input matrices, to output the given matrices and the computed product matrix in a main function. (12) Ans:#include<stdio.h> main() { int A[10][10],B[10][10],C[10][10]; void readmat(int [][],int,int),writemat(int [][],int,int); void matprod(int [][],int [][],int [][],int,int,int); int m,n,p,q; printf("\n Enter the order of the matrix A "); scanf("%d%d",&m,&n); printf("\n Enter the order of the matrix B "); scanf("%d%d",&p,&q); if(n!=p) { printf("\n Matrix multiplication is not possible"); getch(); exit(0); } printf("\n Matrix multiplication is possible"); printf("\n Enter the elements of matrix A \n"); readmat(A,m,n); printf("\n Enter the elements of matrix B \n"); readmat(B,p,q); matprod(A,B,C,m,n,q); printf("\n The elements of matrix A are \n"); writemat(A,m,n); printf("\n The elements of matrix B are \n"); writemat(B,p,q); printf("\n The elements of product matrix are \n"); writemat(C,m,q); getch(); } void readmat(int x[10][10],int m,int n) { int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&x[i][j]); return; } void writemat(int x[10][10],int m,int n) {

int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%5d",x[i][j]); printf("\n"); } return; } void matprod(int x[10][10],int y[10][10],int z[10][10],int m,int n,int q) { int i,j,k; for(i=0;i<m;i++) for(j=0;j<q;j++) { z[i][j]=0; for(k=0;k<n;k++) z[i][j]+=x[i][k]*y[k][j]; } return; } Q17. Write a C program to read N integers and store them in an array A, and so find the sum of all these elements using pointer. (6marks) Ans:#include<stdio.h> main() { int n,a[10],i,sum=0; printf("\n Enter the size of an array "); scanf("%d",&n); printf("\n Enter the elements of the array "); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) sum+=*(a+i); printf("\n The elements of the given array are \n"); for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n The sum of the elements of array=%d",sum); getch(); }

Q18. Write a C Program to input N integers. Write the following functions and use them in sort by selection sort. a. To find maximum of elements b. To swap 2 elements (10marks) Ans:#include <stdio.h> int i,j,temp,n,a[10]; main() { int mpos,max(int,int); void exchange(int,int); clrscr(); printf("Enter value of n\n"); scanf("%d",&n); printf("Enter the elements one by one\n"); for (i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { mpos=max (i,n); exchange(i,mpos); } printf("Sorted array using selection sort is \n"); for(i=0;i<n;i++) printf("\n%d",a[i]); getch(); } int max(int i,int n) { int pos=i; for (j=i+1;j<n;j++) if (a[j]>a[pos]) pos=j; return(pos); } void exchange (int p,int q) { temp=a[p]; a[p]=a[q]; a[q]=temp; return; }

Q19. Write a C program to swap two elements using pointers? Ans:#include <stdio.h> main() { int a,b,*pta,*ptb,temp; scanf(%d %d ,&a,&b); pta=&a; ptb=&b; printf( \n values of a& b before swap are a= %d b= %d, a,b); temp=*pta; *pta = *ptb; *ptb= temp; printf( \n values of a& b after swap are a= %d b= %d, a,b); } ******************

(5marks)

Você também pode gostar