BC0034-Computer Concepts C Programming

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Name : Roll No: Program Code: Center Code: Subject : Semester:

surti Niravkumar Prahaladbhai 1205022165 BCA 3439 (BC0034) Computer Concepts & C Programming 1

May 2012 Bachelor of Computer Application (BCA) Semester 1 BC0034 Computer Concepts & C Programming 4 Credits
(Book ID: B0678)

Assignment Set 1 (40 Marks)

1. How do you declare an integer variable? Explain with the help of one example. Ans. Valerie Variable is a numeric variable. She loves to hold numbers any number; it doesn't matter. Whenever she sees an equal sign, she takes to a value and holds it tight. But see another equal sign, and she takes on a new value. In that way, Valerie is a little flaky. You could say that Valerie's values vary, which is why she's a variable. example, int x; / / here the integer variable x is just declared. Declaration is: int x; double y; char a; Initialization means you are assigning a value to that variable.Ini ti al i ze can refer to the process of starting up a program or system. For example=10; / /here the integer variable x, declared above, is assigned value 10. Initialization is: x =1; double=2.3; char =' w' ; Also you can use declaration and initialization in the same time: int x = 1; double y = 2.3; char a =' w' 2. What are floating point numbers? How are they initialized in C program Explain with the help of one example. Ans. Integers are great for counting whole numbers, but sometimes we need to store very large numbers, or numbers with a fractional component. A floating point type variable is a variable

that can hold a real number, such as 4.0, 2.5, 3.33, or 0.1226. There are three different floating point data types: float, double, and long double. A float is usually 4 bytes and a double 8 bytes, but these are not strict requirements, so sizes may vary. Long doubles were added to the language after its release for architectures that support even larger floating point numbers. But typically, they are also 8 bytes, equivalent to a double. Floating point data types are always signed. The floating part of the name floating point refers to the fact that a floating point number can have a variable number of decimal places. For example, 2.5 has 1 decimal place, whereas 0.1226 has 4 decimal places.When we assign numbers to floating point numbers, it is convention to use at least one decimal place. This helps distinguish floating point values from integer values. 1 int nValue = 5; // 5 means integer 2 float fValue = 5.0; // 5.0 means floating point

3. What are the commonly used input/output functions in C? How are they accessed? Ans. We have already seen that the C language is accompanied by some library functions to handle input/output (I/O) operations. Commonly used input/output functions in C are six I/O functions: getchar0, putchar0, scanf0, printf0, gets0 & puts0 getchar0, putchar0, scanf0, printf0, gets0 and puts0 are the commonly used input/output functions in C. These functions are used to transfer of information between the computer and the standard input/output devices. getchar0 and putchar0 are the two functions to read and write single character. scanf0 and printf0 are the two formatted input/output functions. These functions can handle characters, numerical values and strings as well. gets0 and puts0 functions are used to handle strings. scanf0, printf0, gets0 and puts0 functions are used in interactive programming.

These functions can be accessed within a program by including the header file stdlo.h.

4. State the rules for naming a variable in C. Ans. ~ Variable names in Visual C++ can range from 1 to 255 characters. To make variable names portable to other environments stay within a 1 to 31 character range. ~ All variable names must begin with a letter of the alphabet or an underscore( _ ). For beginning programmers, it may be easier to begin all variable names with a letter of the alphabet. ~ After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed. ~ Uppercase characters are distinct from lowercase characters. Using all uppercase letters is used primarily to identify constant variables. ~ You cannot use a C++ keyword (reserved word) as a variable name.

5. What are the different types of bit operators? Explain with help of example for one bit operator. Ans. The operands for bitwise operators can be any one of the data types of the integer or binary string data type categories (except for the image data type), except that both operands cannot be any one of the data types of the binary string data type category. The following table shows the supported operand data types. Example: if A = 6 (binary 110), and B = 4 (binary 100), the bitwise AND operation will compare each corresponding bit, and result in binary 100 (decimal 4). This can be used to check whether a specific bit, in variable A in this case, is equal to one or zero.

6. Explain pre increment and post increment operator with an example. Ans. Both pre-increment and post-increment ultimately increment the operand. The difference is that during the evaluation of the expression, the results of the intermediate expression for the preincrement case uses the already incremented value, while the post-increment case uses the non incremented value.

An example: int a = 2; int b = 5; int c = a ++ * b; In this case, a will have a value of 3, and c will have a value of 10. If, on the other hand, you said int c = ++ a * b; then c would have a value of 15, while a would still have a value of 3.

7. What are the rules used in naming a variable? Give examples. Ans. n general, variable names are made up of letters (A-Z, a-z), digits (0-9), and underscores ( _ ), with the first character of the variable name not being a digit. Some languages limit the length of the variable name. This covers many languages, but some languages add to this limitation with things like starting variable names with a dollar sign ($) or a percent sign (%). So you need to know the specific rules for the language that you are using. Some languages also have conventions, that are generally followed, but not required by the syntax of the language. For example: if you read a product's installation date from the Registry, naming the variable Greegleborg452sx probably isn't a good idea. Use a variable name like InstallDate instead. 8. How do you convert integer to floating point and vice versa? Explain with one example. Ans. The most common encoding of floating-point numbers uses IEEE 754. For single-precision numbers, there is a sign bit (s), 8 exponent bits (e), and 23 fraction bits (f).

For most values of s, e, and f, the value represented is -1s2e-127F, where F is the number you get by writing 1. followed by the 23 bits of f and then interpreting that string as a binary numeral. E.g., if f is 10000000000000000000000, then the binary numeral is 1.10000000000000000000000, which is (in decimal) 1.5, so F is 1.5.

The above holds whenever 0 < e < 255. The values 0 and 255 are special.

When e is 0, the value represented is the same as above except that you start F with 0. instead of 1.. In particular, if f is zero, then the value represented is zero. If f is not zero, these are called denormal numbers, because they are smaller than the normal values represented in the primary way above.

When e is 255 and f is 0, the value represented is +infinity or -infinity, according to the sign bit, s. When e is 255 and f is not zero, the value represented is called a NaN, Not a Number, which is used for debugging or catching errors or other special purposes. There are quiet NaNs (which do not cause traps; they are typically used when you want to continue calculations to get a final result, then figure out what to do about a NaN) and signaling NaNs (which do cause traps; they are typically used when you want to abort a calculation because an error has occurred).

There may be variations in how the encoding appears on different platforms, especially the ordering of bytes within the 32 bits. And some platforms do not use IEEE 754 encodings.

Double-precision encoding uses the same scheme, except e is 11 bits, the 127 (called the exponent bias) is changed to 1023, and f is 52 bits. Also the special value for the exponent is its 11-bit maximum, 2047, rather than the 8-bit maximum, 255.

May 2012 Bachelor of Computer Application (BCA) Semester 1 BC0034 Computer Concepts & C Programming 4 Credits
(Book ID: B0678)

Assignment Set 2 (40 Marks)

1. Given the length of a side, write a C program to compute surface area and volume of a cube. Ans.
C program for area of a cube Formula of surface area of cube: Surface_area = 6 * a2 Formula of volume of cube: Volume = a3 C code:

#include<stdio.h> int main(){ float a; float surface_area,volume; printf("Enter size of any side of a cube : "); scanf("%f",&a); surface_area = 6 * (a * a); volume = a * a * a; printf("Surface area of cube is: %.3f",surface_area); printf("\nVolume of cube is : %.3f",volume); return 0; } Sample output: Enter size of any side of a cube: 3 Surface area of cube is: 54.000 Volume of cube is: 27.000
2. How do you nest if statements?. Explain with the help of one example.

Ans. Irs also possible to nest oneif statement inside another. (For that matter, irs in general possible to nest any kind of statement or control flow construct within another.) For example,here is a little piece of code which decides roughly which quadrant of the compass you're walking into, based on anx value which is positive if you're walking east, and ay value which is positive if you're walking north:

Example: if(x> 0) { if(y > 0) printf ("Northeast.\n"); else printf("Southeast.\n"); } else { if(y> 0) printf("N orthwest. \n "); else printf("Southwest. \n"); } When youhave onei f statement (or loop) nested inside another, it's a very good idea to use explicit braces { }, as shown, to make it clear (both to you and to the compiler)how they're nested and whichelse goes with which i f. It's also a good idea to indent the various levels, also as shown, to make the code more readable tohumans. Why do both? You use indentation to make the code visually more readable to yourself and otherhumans, but the compiler doesn't pay attention to the indentation (since all whitespace is essentially equivalent and is essentially ignored).T herefore, you alsohave to make sure that the punctuation is right

3.Write a recursive program to solve Towers of Hanoi problem. Ans. #inc1ude<stdio.h> maim) { void Recursive Hanoi(int, char, char, char); int n; printfi"Towers of Hanoi\n\n"); printfi" How many disks?"); scanf("%d", &n); printf("\n"); Recusrive Hanoi(n, 'L', 'R', 'C'); } void Recursive Hanoi(int n, char from, char to, char temp) { 1*Transfer n disks from one pole to anot her *1 1* n= number of disks from=origin to=destination temp=temporary storage*1 {

if(n>O){ 1* move n-I disks from origin to temporary *1 Recursive _ Hanoim-l , from, temp, to); 1* move nth disk from origin to destination *1 printf(" Move disk %d from %c to %c\n", n, from, to); 1* move n-I disks from temporary to destination *1 Recursive_Hanoi(n-l, temp, to, from); } return; } 7. Ques: Explain single dimensional and two dimensional array concepts. Ans. One Dimensional Arrays: So far, we've been declaring simple variables: the declaration int i; declares a single variable, named i, of type int. It is also possible to declare anarr ay of several elements.T he declaration int a[IO]; declares an array, named a, consisting often elements, each of typei nt. Simply speaking, an array is a variable that canhold more than one value. You specify which of the several values you're referring to at any given time by using a numericsubscript. (Arrays in programming are similar to vectors or matrices in mathematics.) We can represent the array a above with a picture like this:

a
~ Multidimensional Arrays or Two dimensional: The C language allows arrays of any dimension to be defined. In this section, we will take a look at two-dimensional arrays. One of the most natural applications for a two dimensional array arises in the case of a matrix. In C, the two-dimensional matrix can be declared as follows: int array [3 ] [6]; Following is the way of declaring as well as initializing two-dimensional arrays. int array[3][6]= { {4,5,6,7,8,9}, {I ,5,6,8,2,4}, {0,4,4,3,1,1 } }; Such arrays are accessed like so:

array[l] [4]= -2; if (array[2][1] > 0) { printf("Element [2][1] is %d", array[2][I]); } Remember that, like ordinary arrays, two-dimensional arrays are numbered from O. Therefore, the array above has elements from array [0] [0] to array [2] [5].
5. Write a program to illustrate the concept of extern variable. Ans.

hint principle= 10000; float rate=5.5; hint time=2; float interest; Type and save the following program in a separate source file calledd em oexter nvar.c #include<stdio.h> #include "externvariables.h"1* the source file where the external variables are defined should be includedhere. *I main( ) { 1* external declarations of the variables which are defined in externvariables.h *1 extern int principle; extern float rate; extern int time; extern float interest; I*computeint er est *I interest= principle*rate*timell 00.0; printf("Interest=%f\n", interest); }
6. Write a program to subtract two 3x3 matrices Ans.

#include<stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j;

printf("Enter the First 3x3 Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf("%d",&a[i][j]); } printf("Enter the Second 3x3 Matrix : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf("%d",&b[i][j]); } printf("\nThe Sum of Matrices is : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; printf("%d ",&c[i][j]; } printf("\n"); } printf("\n\nThe Difference of Matrices is : "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]-b[i][j]; printf("%d ",&c[i][j]; } printf("\n"); } }
7. What is the use of header file? Is the use of header file absolutely necessary? Justify. Ans. A header file is used to define constants, variables, macro's and functions that may be common to several applications. When a system consists of multiple applications that all access the same data it becomes essential that each application uses identical definitions and it is safer for all of those applications to use the same methods to read that data. updating data should only

be performed by a single function, from a single application, but reading data can be safely performed by using the common defintions found in header files. you use header files in the programming language C to declare struct's functions and other things that could be usefull for your program.. it makes thing's simpler and easy to use... ~ It is only strictly necessary to include a file at some point before your first use of some declaration inside that file. So theoretically, you could have them spread thoughout the code. However, one of the things which is useful to be able to do is work out which files a particular module is dependent upon (for say porting to another platform). This is much easier to do if all the includes are together at the start of a file, and much harder if they are scattered. 8. Differentiate Structures and Unions in C programming language with one example. Ans. The difference between structure and union in c are: 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory space

Detailed Example: struct foo { char c; long l; char *p; };

union bar {

char c; long l; char *p; };

You might also like