Vous êtes sur la page 1sur 10

C PROGRAMMING AND DATA STRUCTURES

Important Questions
_________________________________________________________________________

UNIT - III
Pointer and Arrays : Pointers and addresses, Pointers and Arrays, Pointers And function arguments, Address
arithmetic, character pointers and functions, pointers to pointers, multi-dimensional arrays, initialization of
pointer arrays, command line arguments, pointers to functions.

1. What way array is different from an ordinary variable


2. What conditions must be satisfied by the entire elements of any given array?
3.What are subscripts? How are they written? What restrictions apply to the values that can
be assigned to subscripts?
4.What advantage is there in defining an array size in terms of a symbolic constant rather
than a fixed integer quantity?
5.Write a program to find the largest element in an array.
6. Explain how strings can be stored using a multidimensional arrays.
7. What are the string built in functions available? Write in detail about each one of them
with an example.
8. The names of employees of an organization are stored in three arrays, namely, first name,
second name, and last name. Write a program to concatenate the three parts into one string to
be called name.
9. Write a C program to do Matrix Multiplications.
10.Write in detail about one dimensional and multidimensional arrays. Also write about
how initial values can be specified for each type of array?
11. How to use pointer variables in expressions? Explain through examples.
12.Wite a ‘C’ Program to illustrate the use of pointers in arithmetic operations.
13.What is a pointer variable? How is a pointer variable different from an ordinary
variable.
14.Write a C program to read in an array of integers. Instead of using subscripting, however,
employ an integer pointer that points to the elements currently being read in and which is
incremented each time.
15.Explain the process of accessing a variable through its pointer. Give an Example.
16.Write a C program using pointers to read in an array of integers and print its elements
in reverse order.
17.Explain the different ways of representing sparse matrices.
18.Derive the general formula for accessing an element in a three-dimensional array stored in
column-major order.
19.Write a recursive function in C to find the greatest common divisor of two integers.
20.How are initial value written in a one-dimensional array definition? Is the entire
array be initialized? What value is automatically assigned to those array elements not
explicitly initialized?
21. Write a program to calculate mean, variance and standard deviation of n numbers.
S = √variance, where
Variance = 1/n sum (xi – m)2
M = mean of n numbers
22.Write a ‘C’ Program to compute the sum of all elements stored in an array using
pointers.
23.Write a ‘C’ program using pointers to determine the length of a character string.
_________________________________________________________________________

Computer Programming and Data structures 1


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

24. Write a C program using pointer for string comparison


25.Write a C program to arrange the given numbers in ascending order using pointers.
26. Write a C program to find number of words, blank spaces, special characters, digits and
vowels of a given text using pointers.
27.The annual examination is conducted for 50 students for three subjects. Write a
program to read the data and determine the Following:
a) Total marks obtained by each student.
b) The highest marks in each subject and the Roll No. of the student who secured it.
c) The student who obtained the highest total marks.
28. Write a programme to obtain the determinant value of a 3x3 matrix
29. Write a programme to sort all the elements of a 4X4 matrix
30. Write a programme to add two 6X6 matrices
31. Write a programme to read an integer and display its address through integer declaration
and achieve the same through pointers and pointer to pointers.
32. Write a programme to check whether a string is palindrome
33. Write programme to replicate the task carried out by library functions strcpy(), strlen()

_________________________________________________________________________

Computer Programming and Data structures 2


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

UNIT - IV
Structures : Definition, initializing, assigning values, passing of structures as arguments, Arrays of structures,
pointers to structures, self referential structures. Unions, typedef, bit fields, C program examples.

1.What is the main difference between a structure and union in C?


2.Write C functions to perform addition and multiplication operations on two complex
numbers use a structure in C to represent a complex number.
3.Write a C program to display the contents of a text file.
4.Explain with example how a structure can be organized in the ‘C’ language?
5. Write a C program to print maximum marks in each subject along with the name of the
students by using structures. Take 3 subjects and 3 students records.
6. Explain the different ways of passing structure as arguments in functions.
7. Write a C program to illustrate the method of sending an entire structure as a parameter
to a function.
8.How to compare structure variables? Give an example.
9.Define a structure type struct ABS, that contains name, age, designation, and salary.
Using this structure, write a C program to read this information for one person from the
keyboard and print the same on the screen.
10.Write a C program to illustrate the use of structure pointer.
11.Explain the effects of the following statements:
(i). int a, *b = &a; (ii). int p, *p; (iii). char *s; (iv). a = (float*)&X;
12.How are structure elements accessed using pointer? Which operator is used? Give an
example.
13.Write a program to use structure within union. Display the contents of structure elements.
14.Write a C program to copy the contents of a text file to another file. The program should
take the file names (source file and destination file names) from the command-line.
15.Consider a structure master includes the information like name, code, pay, experience.
Write a program to delete and display the information contained in master variables for a
give code.
16.A company markets Hardware items. Create a structure “hwItem” that stores the title
of the item, it’s price, an array of three floats so that it can record the sale in rupees of
a particular item for the last three months, category of the item and it’s original equipment
manufacturer. Write a short program that provided facility to read N no. of items
information, append new item, and displays all records.
17. Illustrate the declaration of three character pointer variables using typedef and also using
a macro definition
18. Create a structure to store the gender of person, marital status (single, married, divorced
or widowed), one of the eight branches and can choose any 15 subjects. Using bitfields
highlight the C language’s memory optimization.
19. Write a programme to read an integer and using shift operator convert the same to binary
format.
20. Give an example for OR, AND and SHIFT operators highlighting their advantage.
21. What is a pointer to a function ? Give an example of its usage highlighting its advantage.

_________________________________________________________________________

Computer Programming and Data structures 3


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

UNIT - V
Console & File I/O : Standard I/O, Formatted I/O, opening & closing of files, I/O operations on files.

1.What are the commonly used input/ output functions in C? How are they accessed?
2.What is the purpose of getchar, putchar functions? How is it used in a C Program? How is
it different from getc () and putc () functions?
3. What is the difference between getchar(), fgetchar(), getch(), getche().
4. What is the difference between printf(), sprintf(), fprintf(), write(), fwrite().
5. Write xgets() and xputs() which work similar to the standard library function gets() and
puts()
6. Write down a function getint() which would receive a numeric string from the keyboard,
convert it to an integer number and return the integer to the calling function. A sample usage
of getint() is shown below:

main(0
{
int a;
a=getint();
printf(“you entered %d”,a);
}
7. What are the arguments that can be used with function main()? Give an illustrative
example for main function arguments.
8. Write a programme to count the number of words in a file.
9. Write a programme to remove all comments from a file.
10.Write a programme to remove all blanks from a file.
11.Write a programme to store every character typed at the keyboard into a file. The
procedure should come to an end as soon as the character ~ is hit from the keyboard.
12.Write a C program to write a string to a file and read the same from the file
simultaneously counting the number of blank spaces, lines and characters in the string
13. Write a programme to encrypt / decrypt a file using:

(a) An offset cipher: In an offset cipher each character from the source file is offset with a
fixed value and then written to the target file. For example, if character read from the
source file is ‘A’, then convert this into a new character by offsetting ‘A’ by a fixed
value say 128, and then writing the new character to the target file.
(b) A substitution cipher: In this each character read from the source file substituted by a
predetermined character and this character is written to the target file. For example, if
character ‘A’ is to be substituted by ‘!’, then a’!’ would be written to the target file in
place of every ‘A’. Similarly every ‘B’ would be substituted by ‘5’ and so on (as
decided by the encryptor)
14. Illustrate the terms NULL and EOF in the context of file operations

_________________________________________________________________________

Computer Programming and Data structures 4


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

/* Usage of command line arguments*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

main(int argc, char* argv[])

{ char ch;
FILE *fin,*fout;

clrscr();

if(argc!=3)
{
puts("insufficient arguments kindly renter");
exit(0);
}

fin = fopen(argv[1],"r");

if(fin==NULL)
{puts("cannot open source file");
exit(0);
}

fout = fopen(argv[2],"w");

if(fout==NULL)
{puts("cannot open target file");
exit(0);
}
while(1)
{
ch=fgetc(fin);
if(ch==EOF)break;
fputc(ch,fout);
}
fclose(fin);
fclose(fout);
getch();

_________________________________________________________________________

Computer Programming and Data structures 5


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

/* Usage of pointer to pointer*/

#include <stdio.h>
#include <conio.h>

main()

{ int *ptr,**ptr1,a=5;

clrscr();

ptr=&a;
ptr1=&ptr;
printf("1.%d\n",a);
printf("2.%d\n",*ptr);
printf("3.%d\n",**ptr1);
printf("1.%u\n",&a);
printf("2.%u\n",ptr);
printf("3.%u\n",*ptr1);

scanf("%d",ptr);
printf("1.%d\n",a);
scanf("%d",*ptr1);
printf("1.%d\n",a);

getch();

_________________________________________________________________________

Computer Programming and Data structures 6


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

/* to show bitfields of an integer */ /*to show bitfields of for any integer */


#include <stdio.h> #include <stdio.h>
#include <conio.h> #include <conio.h>
void showbits(int n) # include <limits.h>
{ void bit_print(int n)
int i,k,andmask; {
for(i=15;i>=0;i--) int i;
{ int k= sizeof(int)*CHAR_BIT;
andmask= 1<<i; int andmask=1<<(k-1);
k=n & andmask; for(i=1;i<=k;++i)
k==0 ? printf("0"):printf("1"); {
} putchar(((n&andmask)==0) ? '0' : '1');
} n <<=1;
if(i%CHAR_BIT == 0 && i<k)
putchar(' ');
}
}
/* main programme */ /* main programme */
main() main()
{ int n; { int n;
void showbits(int ); void bit_print(int );
clrscr(); clrscr();

printf(" Enter a Number : "); printf(" Enter a Number : ");


scanf("%d",&n); scanf("%d",&n);
printf(" \n The bitpattern for %d is : ",n); printf(" \n The bitpattern for %d is : ",n);

showbits(n); bit_print(n);

getch(); getch();

} }

_________________________________________________________________________

Computer Programming and Data structures 7


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

MEMORY ALLOCATION (SMA, DMA)

/* Program 1. usage of arrays in Memory Allocation */

#include <stdio.h>
#include <conio.h>
main()
{int i,count=0;
char name[11],search; /* array size is defined in the main*/
char* cp;
clrscr();

printf("enter a string with max length less then 10characters : ");


scanf("%s",name);
name[10]=0;
printf("enter the character to be searched : ");
fflush(stdin);
search=getchar();
/* scanf(" %c",&search); try using this instead of above two lines*/
/* remove the space before %c and check for result and analyse*/
cp=name;
while(*cp!=NULL)
{if (*cp==search)count++;
cp++;
}
printf("\n\nIn the string \"%s\"\n",name);
printf("\n\t\tcharacter %c appears %d times. ",search,count);
getch();
}
_______________________________________________________________________

/* Program 2. usage of arrays in Memory Allocation */

#define MAX 11 /* change when required, it is only at one place*/


#include <stdio.h>
#include <conio.h>
main()
{int i,count=0;
char name[MAX],search;
char* cp;
clrscr();

printf("enter a string with max length less then 10characters : ");


scanf("%s",name);
name[MAX -1]=0;
_________________________________________________________________________

Computer Programming and Data structures 8


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

printf("enter the character to be searched : ");


fflush(stdin);
search=getchar();
/* scanf(" %c",&search); try using this instead of above two lines*/
/* remove the space before %c and check for result and analyse*/
cp=name;
while(*cp!=NULL)
{if (*cp==search)count++;
cp++;
}
printf("\n\nIn the string \"%s\"\n",name);
printf("\n\t\tcharacter %c appears %d times. ",search,count);
getch();
}

_____________________________________________________________________

/* Program 3. use of malloc – dynamic memory allocation */

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{ char *cp,*dup,search;
int count=0,len=0;
clrscr();
dup=cp=malloc(11*sizeof(char));
fflush(stdin);
printf("Enter a character to be searched :\n");
scanf("%c",&search);
fflush(stdin);
printf("Enter a string length less than 10 characters :\n");

while ( (*cp=getchar())!=10 && (len<10) )


{
if(*cp==search)count++;
cp++;
len++;
}
*cp='\0';

printf("\n\nIn string \"%s\"\n",dup);


printf("\t\t\t\'%c\' appears %d times",search,count);

_________________________________________________________________________

Computer Programming and Data structures 9


C PROGRAMMING AND DATA STRUCTURES
Important Questions
_________________________________________________________________________

getch();
}
____________________________________________________________

/* Program 4. use of malloc where chunk size is dynamically entered*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{ char *cp,*dup,search;
int count=0,len=0,siz;
clrscr();
printf("Enter thesize ofthe chunk in positive integer : ");
scanf("%d",&siz);
dup=cp=malloc(siz*sizeof(char));
fflush(stdin);
printf("Enter a character to be searched :\n");
scanf("%c",&search);
fflush(stdin);
printf("Enter a string length less than %d characters :\n",siz-1);

while ( (*cp=getchar())!=10 && (len<(siz-1)) )


{
if(*cp==search)count++;
cp++;
len++;
}
*cp='\0';

printf("\n\nIn string \"%s\"\n",dup);


printf("\t\t\t\'%c\' appears %d times",search,count);

getch();
}

_________________________________________________________________________

Computer Programming and Data structures 10

Vous aimerez peut-être aussi