Search This Blog

Thursday, May 31, 2012

introduction to array in c

What is an Array in C Language?

An array in C Programing Language can be defined as number of memory locations, each of which can store the same data type and which can be references through the same variable name.
An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a “string”, where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of 10 numbers, of which 5 are ints and 5 are floats.
Arrays and pointers have a special relationship as arrays use pointers to reference memory locations.

Declaration of an Array

Arrays must be declared before they can be used in the program. Standard array declaration is as

type variable_name[lengthofarray];
 
Here type specifies the variable type of the element which is going to be stored in the array. In C programmin language we can declare the array of any basic standard type which C language supports. For example

double height[10];
float width[20];
int min[9];
char name[20];
 
In C Language, arrays starts at position 0. The elements of the array occupy adjacent locations in memory. C Language treats the name of the array as if it were a pointer to the first element This is important in understanding how to do arithmetic with arrays. Any item in the array can be accessed through its index, and it can be accesed any where from with in the program. So
m=height[0];
variable m will have the value of first item of array height.
The program below will declare an array of five integers and print all the elements of the array.

int myArray [5] = {1,2,3,4,5};
/* To print all the elements of the array
for (int i=0;i<5;i++){
   printf("%d", myArray[i]);
}

Initializing Arrays

Initializing of array is very simple in c programming. The initializing values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Here is an example which declares and initializes an array of five elements of type int. Array can also be initialized after declaration. Look at the following C code which demonstrate the declaration and  initialization of an array.

int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement
int studentAge[4];
studentAge[0]=14;
studentAge[1]=13;
studentAge[2]=15;
studentAge[3]=16;

Performing operations on Arrays

Here is a program that will demonstrate the simple operations of the array.

#include <stdio.h>
void oneWay(void);
void anotherWay(void);
int main(void) {
   printf("\noneWay:\n");
   oneWay();
   printf("\nantherWay:\n");
   anotherWay();
}

/*Array initialized with aggregate */
void oneWay(void) {
   int vect[10] = {1,2,3,4,5,6,7,8,9,0};
   int i;
   for (i=0; i<10; i++){
       printf("i = %2d vect[i] = %2d\n", i, vect[i]);
   }
}

/*Array initialized with loop */
void anotherWay(void) {
   int vect[10];
   int i;
   for (i=0; i<10; i++)
        vect[i] = i+1;
   for (i=0; i<10; i++)
        printf("i = %2d vect[i] = %2d\n", i, vect[i]);
}

/* The output of this program is
   oneWay:
   i = 0 vect[i] = 1
   i = 1 vect[i] = 2
   i = 2 vect[i] = 3
   i = 3 vect[i] = 4
   i = 4 vect[i] = 5
   i = 5 vect[i] = 6
   i = 6 vect[i] = 7
   i = 7 vect[i] = 8
   i = 8 vect[i] = 9
   i = 9 vect[i] = 0

   antherWay:
   i = 0 vect[i] = 1
   i = 1 vect[i] = 2
   i = 2 vect[i] = 3
   i = 3 vect[i] = 4
   i = 4 vect[i] = 5
   i = 5 vect[i] = 6
   i = 6 vect[i] = 7
   i = 7 vect[i] = 8
   i = 8 vect[i] = 9
   i = 9 vect[i] = 10
   */
Here is a more complex program that will demonstrate how to read, write and traverse the integer arrays

#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);

int main(void) {
   int x[10];
   int hmny;

   hmny = getIntArray(x, 10, 0);
   printf("The array was: \n");
   printIntArray(x,hmny);
   reverseIntArray(x,hmny);
   printf("after reverse it is:\n");
   printIntArray(x,hmny);
}

void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
   int temp = *x;
   *x = *y;
   *y = temp;
}

/* n is the number of elements in the array a.
* These values are printed out, five per line. */
void printIntArray(int a[], int n){
   int i;
   for (i=0; i<n; ){
      printf("\t%d ", a[i++]);
      if (i%5==0)
        printf("\n");
   }
   printf("\n");
}

/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
int getIntArray(int a[], int nmax, int sentinel)
{
   int n = 0;
   int temp;

   do {
     printf("Enter integer [%d to terminate] : ", sentinel);
     scanf("%d", &temp);
     if (temp==sentinel) break;
     if (n==nmax)
       printf("array is full\n");
     else
     a[n++] = temp;
   }while (1);
   return n;
}

/* It reverse the order of the first n elements of array */
void reverseIntArray(int a[], int n)
{
   int i;
   for(i=0;i<n/2;i++){
     intSwap(&a[i],&a[n-i-1]);
   }
}

Copy one array into another

There is no such statement in C language which can directly copy an array into another array. So we have to copy each item seperately into another array.

#include <stdio.h>
int main()
{
   int iMarks[4];
   short newMarks[4];
   iMarks[0]=78;
   iMarks[1]=64;
   iMarks[2]=66;
   iMarks[3]=74;
   for(i=0; i<4; i++)
     newMarks[i]=iMarks[i];
   for(j=0; j<4; j++)
     printf("%d\n", newMarks[j]);
   return 0;
}
To summarize, arrays are provides a simple mechanism where more than one elements of same type are to be used. We can maintain, manipulate and store multiple elements of same type in one array variable and access them through index.

Sunday, December 11, 2011

test on c

Q1.
main()
{
int i;
clrscr();
printf("%d", &i)+1;
scanf("%d", i)-1;
}

a. Runtime error.

b. Runtime error. Access violation.
c. Compile error. Illegal syntax
d. None of the above





Ans: d,printf( ) prints address/garbage of i,

scanf() dont hav & sign, so scans address for i
+1, -1 dont hav any effect on code

Q2.

main(int argc, char *argv[])
{
 (main && argc) ? main(argc-1, NULL) : return 0;
}

a. Runtime error.

b. Compile error. Illegal syntax
c. Gets into Infinite loop
d. None of the above



Ans: b) illegal syntax for using return


Q3.

main()
{
 int i;
 float *pf;
 pf = (float *)&i;
*pf = 100.00;
 printf("
%d", i);
}

a. Runtime error.

b. 100
c. Some Integer not 100
d. None of the above




Ans: d) 0


Q4.


main()

{
 int i = 0xff ;
 printf("
%d", i<<2);
}

a. 4

b. 512
c. 1020
d. 1024




Ans: c) 1020



Q5.

#define SQR(x) x * x

main()

{
 printf("%d", 225/SQR(15));
}

a. 1

b. 225
c. 15
d. none of the above



Ans: b) 225



Q6.

union u
{
 struct st
{
 int i : 4;
 int j : 4;
 int k : 4;
 int l;
}st;
int i;
}u;

main()

{
 u.i = 100;
 printf("%d, %d, %d",u.i, u.st.i, u.st.l);
}

a. 4, 4, 0

b. 0, 0, 0
c. 100, 4, 0
d. 40, 4, 0



Ans: c) 100, 4, 0


Q7.

union u
{
 union u
 {
 int i;
 int j;
 }a[10];
 int b[10];
}u;

main()

{
 printf("
%d", sizeof(u));
 printf(" %d", sizeof(u.a));
// printf("%d", sizeof(u.a[4].i));
}

a. 4, 4, 4

b. 40, 4, 4
c. 1, 100, 1
d. 40 400 4


Ans: 20, 200, error for 3rd printf


Q8.

main()
{
 int (*functable[2])(char *format, ...) ={printf, scanf};
 int i = 100;

(*functable[0])("%d", i);

 (*functable[1])("%d", i);
 (*functable[1])("%d", i);
 (*functable[0])("%d", &i);

}


a. 100, Runtime error.

b. 100, Random number, Random number, Random number.
c. Compile error
d. 100, Random number



Q9.

main()
{
 int i, j, *p;
 i = 25;
 j = 100;
 p = &i; // Address of i is assigned to pointer p
 printf("%f", i/(*p) ); // i is divided by pointer p
}

a. Runtime error.

b. 1.00000
c. Compile error
d. 0.00000




Ans: c) Error becoz i/(*p) is 25/25 i.e 1 which is int & printed as a float,

So abnormal program termination,
runs if (float) i/(*p) -----> Type Casting

Q10.

main()
{
 int i, j;
 scanf("%d %d"+scanf("%d %d", &i, &j));
 printf("%d %d", i, j);
}

a. Runtime error.

b. 0, 0
c. Compile error
d. the first two values entered by the user



Ans: d) two values entered, 3rd will be null pointer assignment


Q11.


main()

{
 char *p = "hello world";
 p[0] = 'H';
 printf("%s", p);
}

a. Runtime error.

b. "Hello world
c. Compile error
d. "hello world




Ans: b) Hello world


Q12.

main()
{
 char * strA;
 char * strB = I am OK;
 memcpy( strA, strB, 6);
}

a. Runtime error.

b. I am OK
c. Compile error
d. I am O



Ans: c) I am OK is not in " "



Q13. How will you print % character?

a. printf("\%)
b. printf("\%)
c. printf("%%)
d. printf("\%%)


Ans: c) printf(" %% ");


Q14.

const int perplexed = 2;
#define perplexed 3

main()

{
 #ifdef perplexed
 #undef perplexed
 #define perplexed 4
 #endif
 printf("%d",perplexed);
}

a. 0

b. 2
c. 4
d. none of the above



Ans: c)


Q15.

struct Foo
{
 char *pName;
};

main()

{
 struct Foo *obj = malloc(sizeof(struct Foo));
 clrscr();
 strcpy(obj->pName,"Your Name");
 printf("%s", obj->pName);
}

a. Your Name

b. compile error
c. Name
d. Runtime error




Ans a)


Q16.

struct Foo
{
 char *pName;
 char *pAddress;
};

main()

{
 struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
 obj->pName = malloc(100);
 obj->pAddress = malloc(100);

strcpy(obj->pName,"Your Name");

 strcpy(obj->pAddress, "Your Address");

free(obj);

 printf("%s", obj->pName);
 printf("%s", obj->pAddress);
}

a. Your Name, Your Address

b. Your Address, Your Address
c. Your Name Your Name
d. None of the above






Ans: d) printd Nothing, as after free(obj), no memory is there containing

obj->pName & pbj->pAddress

Q17.

main()
{
 char *a = "Hello ";
 char *b = "World";
clrscr();
 printf("%s", strcat(a,b));
}

a. Hello

b. Hello World
c. HelloWorld
d. None of the above




Ans: b)


Q18.


main()

{
 char *a = "Hello ";
 char *b = "World";
 clrscr();
 printf("%s", strcpy(a,b));
}

a. "Hello

b. "Hello World
c. "HelloWorld
d. None of the above







Ans: d) World, copies World on a, overwrites Hello in a.


Q19.

void func1(int (*a)[10])
{
 printf("Ok it works");
}

void func2(int a[][10])

{
 printf("Will this work?");
}

main()
{
 int a[10][10];
 func1(a);
 func2(a);
}

a. Ok it works

b. Will this work?
c. Ok it worksWill this work?
d. None of the above







Ans: c)


Q20.

main()
{
 printf("%d, %d", sizeof('c'), sizeof(100));
}

a. 2, 2

b. 2, 100
c. 4, 100
d. 4, 4





Ans: a) 2, 2


Q21.

main()
{
 int i = 100;
 clrscr();
 printf("%d", sizeof(sizeof(i)));
}

a. 2

b. 100
c. 4
d. none of the above




Ans: a) 2


Q22.  


main()

{
 int c = 5;
 printf("%d", main||c);
}

a. 1

b. 5
c. 0
d. none of the above



Ans: a) 1, if we use main|c then error, illegal use of pointer



Q23.

main()
{
 char c;
 int i = 456;
 clrscr();
 c = i;
 printf("%d", c);
}

a. 456

b. -456
c. random number
d. none of the above







Ans: d) -56


Q24.

void main ()
{
 int x = 10;
 printf ("x = %d, y = %d", x,--x++);
}

a. 10, 10

b. 10, 9
c. 10, 11
d. none of the above






Ans: d) Lvalue required


Q25.

main()
{
 int i =10, j = 20;
 clrscr();
 printf("%d, %d, ", j-- , --i);
 printf("%d, %d ", j++ , ++i);
}

a. 20, 10, 20, 10

b. 20, 9, 20, 10
c. 20, 9, 19, 10
d. 19, 9, 20, 10







Ans: c)


Q26.


main()

{
 int x=5;
 clrscr();

for(;x==0;x--) {

 printf("x=%d
", x--);
 }
}

a. 4, 3, 2, 1, 0

b. 1, 2, 3, 4, 5
c. 0, 1, 2, 3, 4
d. none of the above





Ans: d) prints nothing, as condition x==0 is False


Q27

main()
{
 int x=5;

for(;x!=0;x--) {

 printf("x=%d
", x--);
 }
}

a. 5, 4, 3, 2,1

b. 4, 3, 2, 1, 0
c. 5, 3, 1
d. none of the above







Ans: d) Infinite loop as x is decremented twice, it never be 0

and loop is going on & on

Q28
main()
{
 int x=5;
 clrscr();

for(;x<= 0;x--)

{
 printf("x=%d ", x--);
 }
}
a. 5, 3, 1
b. 5, 2, 1,
c. 5, 3, 1, -1, 3
d. "3, -1, 1, 3, 5




Ans: prints nothing, as condition in loop is false.


Q29.

main()
{
 {
 unsigned int bit=256;
 printf("%d", bit);
 }
 {
 unsigned int bit=512;
 printf("%d", bit);
 }
}

a. 256, 256

b. 512, 512
c. 256, 512
d. Compile error



Ans: 256, 512, becoz these r different blocks, so declaration allowed


Q30.

main()
{
 int i;
 clrscr();
 for(i=0;i<5;i++)
 {
 printf("%d
", 1L << i);
 }
}
a. 5, 4, 3, 2, 1
b. 0, 1, 2, 3, 4
c. 0, 1, 2, 4, 8
d. 1, 2, 4, 8, 16




Ans: d) L does't make any diff.


Q31.

main()
{
 signed int bit=512, i=5;

for(;i;i--)

 {
 printf("%d
", bit = (bit >> (i - (i -1))));
 }
}

a. 512, 256, 128, 64, 32

b. 256, 128, 64, 32, 16
c. 128, 64, 32, 16, 8
d. 64, 32, 16, 8, 4



Ans: b)


Q32.

main()
{
 signed int bit=512, i=5;

for(;i;i--)

 {
 printf("%d
", bit >> (i - (i -1)));
 }
}

a. 512, 256, 0, 0, 0

b. 256, 256, 0, 0, 0
c. 512, 512, 512, 512, 512
d. 256, 256, 256, 256, 256




Ans: d) bit's value is not changed


Q33.

main()
{
 if (!(1&&0))
 {
 printf("OK I am done.");
 }
 else
 {
printf("OK I am gone.");
 }
}

a. OK I am done

b. OK I am gone
c. compile error
d. none of the above





Ans: a)


Q34

main()
{
 if ((1||0) && (0||1))
 {
 printf("OK I am done.");
 }
 else
 {
printf("OK I am gone.");
 }
}

a. OK I am done

b. OK I am gone
c. compile error
d. none of the above





Ans: a)


Q35

main()
{
 signed int bit=512, mBit;

{

mBit = ~bit;
bit = bit & ~bit ;

printf("%d %d", bit, mBit);

 }
}

a. 0, 0

b. 0, 513
c. 512, 0
d. 0, -513



Ans: d)

What is Multimedia ?

What is Multimedia ?


Multimedia is collection of text,image,audio,video,animation that are displayed to the user via some digital or mean.

Components of multimedia

*Text
*Image
*Audio
*Video
*Animation

Now some description about its component:-



Text:-the use of text , we can see everywhere in out life , to convey something we use text, to show something we use text and many more place we use it.
basically the term text includes alphabets,numeric,special character,etc.

a-z, A-Z, 0-9, !@#$%^&*, etc.


we can see the use text in textbooks , newspaper and many more places.font is also a part of text.but in font we include its property like bold , italic , underline , forecolor etc.


types of fonts:-


serif:-fonts which have tail at the end of each character , known as serif font.it is very useful for hardcopy thats why we see some textbooks containing serif fonts
example:-times new roman,bookman and so on


sans serif:-often used on webpages and does not have tails at the of each character
example:-arial,verdana and so on

Sunday, July 3, 2011

find out number vowel in a string

find out number vowel in a string

#include<stdio.h>
#include<conio.h>
void main()
{
char a[]="array";
int i,v=0,c=0;
for(i=0;a[i]!=NULL;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
v++;
}
else
c++;
}
printf("there are %d vowel and %d consonant",v,c);
getch();
}

Calculate lenth of a String

Calculate lenth of a string without using Library Function

#include<stdio.h>
#include<conio.h>
void main()
{
char a[]="ram";
int i,c=0;
for(i=0;a[i]!=NULL;i++)
{
c++;
}
printf("lenth of the string is %d",c);
getch();
}

Sunday, June 26, 2011

Add two Numbers witout using + sign

Add two Numbers without using '+' sign

#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=10,c;
c=a-(-b);
printf("total of a and b is %d",c);
getch();
}


Wednesday, June 8, 2011

Important shortcut keys often used in window

Shortcut Key       Desscription


Alt + F                    File menu options in current program.


Alt + E                    Edit options in current program


F1                          Universal Help in almost every Windows program.


Ctrl + A                  Select all text.


Ctrl + X                  Cut selected item.


Shift + Del               Cut selected item.


Ctrl + C                  Copy selected item.


Ctrl + Ins                Copy selected item


Ctrl + V                  Paste


Shift + Ins               Paste


Home                      Goes to beginning of current line.


Ctrl + Home            to beginning of document.


End                         Goes to end of current line.


Ctrl + End               Goes to end of document.


Shift + Home           Highlights from current position to beginning of line.


Shift + End              Highlights from current position to end of line.


Ctrl + Left arrow      Moves one word to the left at a time.


Ctrl + Right arrow    Moves one word to the right at a time.