Вы находитесь на странице: 1из 7

i n t m a i n ( i n t , c h a r * a r g v [ ] ) { i n t i , f o r ( i = 0 ; i < a r g ; + +

July 2011 Master of Computer Application (MCA) Semester 1 MC0061 Computer Programming C Language 4 Credits
(Book ID: B0678 & B0679)

Assignment Set 1 (40 Marks)


Answer all Questions Book ID: B0678

Question 1: Define the Function prototype? Write a C Program to solve the Towers of Hanoi Problem? Answer 1: A function prototype is a declaration in C and C++ of a function, its name, parameters and return type. Unlike a full definition, the prototype terminates in a semi-colon. int getsum(float * value) ; Prototypes are used in header files so that external functions in other files can be called and the compiler can check the parameters during compilation. The C Code for Towers of Hanoi Program: #include "stdio.h" void towers(int,char,char,char); void towers(int n,char frompeg,char topeg,char auxpeg) { /* If only 1 disk, make the move and return */ if(n==1) { printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ towers(n-1,frompeg,auxpeg,topeg); /* Move remaining disks from A to C */ printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg); /* Move n-1 disks from B to C using A as auxiliary */ towers(n-1,auxpeg,topeg,frompeg); } main()

{ int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n\n"); towers(n,'A','C','B'); return 0; } Question 2: With the help of suitable examples explain type conversions using various data types? Answer 2: Type conversion occurs when the expression has data of mixed data types. example of such expression include converting an integer value in to a float value, or assigning the value of the expression to a variable with different data type. In type conversion, the data type is promoted from lower to higher because converting higher to lower involves loss of precision and value. Explicit type conversions can be forced in any expression , with a unary operator called a cast. Example int n; float x; x=(float)n; The above statement will convert the value of n to a float value before assigning to x.but n is not altered For example,consider the following assignment statement int a; float b; a=5.5; b=100; In the first statement a=5.5 ;a is declared as int so the float value 5.5 cannot be stored in a.In such a case float is demoted to an int and then its value is stored. hence 5 is stored in a. In the second statement b=100;since b is a float variable 100 is promoted to 100.000000 and then stored in b. Question 3: Using input and output functions in C, write a program to accept a string of characters Answer 3: // using the gets() and putchar() #include <stdio.h> #include <stdlib.h> // function prototype... void reverse(char *); int main()

{ // an array for storing the string... char sentence[80]; printf("Using gets() and putchar()\n"); printf("--------------------------\n"); // prompt for user input... printf("Enter a line of text:\n"); // gets_s(*buffer, size_in_character); // gets_s(sentence, 79); // a secure version gets(sentence); printf("\nThe line printed backward is:\n"); // reverse() function call... reverse(sentence); printf("\n"); return 0; } void reverse(char *s) { // test if nothing entered... if(s[0] == '\0') return; // if something entered... else { reverse(&s[1]); putchar(s[0]); } return 0; } Question 4: Explain the following operators with an example for each: a. Conditional Operators b. Bitwise Operators c. gets() and puts() function with a programming example of each Answer 4. a. Conditional Operators: Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if-else condition phrases. Conditional operators shape is: Condition_phrase ? phrase1 : phrase2; If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value.

Example: int a, b, c; ... c = a > b ? a : b; // if a>b "execute" a, else b b. Bitwise Operators : The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|) operations. Operator & The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. ^ The bitwise-exclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. |The bitwise-inclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Example: short i = 0xAB00; short j = 0xABCD; short n; n = i & j; The result assigned to n in this first example is the same as i (0xAB00 hexadecimal). n = i | j; n = i ^ j; C. gets() and puts() function : puts() Function writes the string to the standard output device. The null terminator is translated to a newline. Declaration int puts(const char *str); Return returns a nonnegative value on success or an EOF upon failure. #include <stdio.h> #include <string.h>

int main(void) { char str[80]; strcpy(str, "this is an example"); puts(str); return 0; } The gets function takes one parameter, the string in which to store the data read. It reads characters from standard input up to the next newline character (that is, when the user presses <RETURN>), discards the newline character, and copies the rest into the string passed to it. If there was no error, it returns the same string (as a return value, which may be discarded); otherwise, if there was an error, it returns a null pointer.

#include <stdio.h> void main() { char initial[2] = { 0 }; char name[80] = { 0 }; printf("Your first initial: "); gets(initial); printf("Your name: " ); gets(name); if(initial[0] != name[0]) printf("\n%s,you got your initial wrong.\n", name); else printf("\nHi, %s. Your initial is correct. Well done!\n", name); } Book ID: B0679 Question 5: Write a program in C to explain pointer arithmetic Answer 5: In c a pointer is a variable that points to or references a memory location in which DATA is stored. Each memory cell in the computer has an address that can be used to access that variable points to a memory location we can access and change the contents of this memory location via the pointer. A pointer is a variable that contains the memory location of another variable.

The syntax is as shown below. You start by specifying the type of DATA stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. type * variable name Example: int *ptr; float *string; Address operator: Once we declare a pointer variable we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following example: ptr=&num; This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260. /* Aprogram to illustrate pointer declaration*/ main() { int *ptr; int sum; sum=45; ptr= printf (\n Sum is %d\n, sum); printf (\n The sum pointer is %d, ptr); } Question 6: Write a program demonstrating the usage of pointers with one dimensional and two dimensional arrays? Answer 6: // Code showing potential use of 1-D Array. #include <stdio.h> int main () { int value [1000],i; // Declaring an Array for (i = 0; i<1000; i++) { value [i] = i; // Loading the Array } for (i = 0; i<1000; i++) { printf ("\n%d",value [i]); // Displaying the Array } return 0; } // Code showing potential use of 2-D Array.

#include <stdio.h> int main () { int value [100] [100],i,j; // Declaring an Array for (j = 0; j<100; j++) { for (i = 0; i<100; i++) { value [j] [i] = j*100+i; // Loading the Array } } for (j = 0; j<100; j++) { for (i = 0; i<100; i++) { printf ("\n%d",value [j] [i]); // Displaying the Array } } return 0; }

Вам также может понравиться