CSE Rockz Home Blog Links Forum VSF

Monday, February 2, 2009

BRAINY BYTES 2 

IN ORDER TO HANDLE PROCESSING SATISFACTORILY AND TO SPEED UP THE RATE OF INFORMATION TRANSFER COMPUTER USES A SPECIAL MEMORY DEVICES CALLED REGISTERS.

LENGTH OF A REGISTER EQUALS TO THE NUMBER OF BITS IT CAN STORE.COMPUTER USES RIGISTRES IN A VARIETY OF APPLICATIONS SO THERE A RE MANY TYPES OF REGISTRES;

MAIN TYPES ARE:

MEMORY ADRESS REGISTER: HOLDS ADRESS OF ACTIVE MEMORY LOCATION 

MEMORY BUFFER REGISTER : HOLDS CONTENTS OF MEMORY WORD READ FROM OR WRITTEN IN THE MEMORY.A WORD TO BE STORED IN A MEMORY LOCATION MUST FIRST BE PLACED IN THIS REGISTER

PROGRAM CONTROL REGISTER:HOLDS ADRESS OF NEXT INSTRUCTION AND EXECUTES STEP BY STEP PROCESS AND CAUSES COMPUTER TO READ SUCCESIVE INSTRUCTIONS .

ACCUMULATOR REGISTOR:
HOLDS INITIAL DATA TO BE OPERATED AND INTERMEDIATE RESULTS AND ALSO FINAL RESULTS  IT IS USED DURING EXECUTION OF INSTRUCTIONS.

INSTRUCTION REGISTOR:
HOLDS CURRENT INSTRUCTION.AS SOON AS INSTRUCTION IS COMPLETED ADRESS AND THE OPERATION ARE SEPERATED:

I/P OR O/P REGISTER: USED TO COMMUNICATE THROUGH INPUT AND OUTPUT DEVICES

CHALLENGES : 2


work out these programs befor any one else does!!

1.wap which accepts a program and prints first nonzero number of its factorial from right side.

2.wap which accepts a line and a string and tells whether string is there in the line or not.

3.wap which accepts a line and two strings and replace all occurances of 1st string in the line with the second one

4.wap which accepts any number from 0 to 999999999 and convrets it to words

5. wap which accepts a year and prints calender of that year with similar indentation to that of a real calendar

6. wap which accepts DOB and prints week of that date

Saturday, January 3, 2009

Daily3

Arrays..

An array is a systamatic arrangement of things...in a scheduled order and a particular defined address...

key points:

  • arrays in c are used to contain,declare and use multiple variables
  • it becomes easy to group a certain number of variables under a single domain called an arry
  • so in this ways arrays are very benefitial for the case of multiple variables
  • arrays can be multi dimensional
  • consider a normal single dimensional  integer array and every integer occupies 2 bytes so as soon as an array of n variables is declared the systems reserves n*2 bytes of memmory under the array name say x now let us assume that first integer value is stored in a array adderss a0. so the 2nd integer will be stored at a0+2 so  nth integer is stored at ao+2*n.so general adders formula can be an= ao+t*n; where the t represents minimum size of variable eg: for float its 4
  • array declaration is done like this 
main()
{
int a[10];
}
  • array declaration should be specific ie: we cannot declare an array containing variable number of its contents eg: we cannot declare array as  int a[i] as the number of variables in it should be a constant 
  • we can read a array by writing a for loop by reading element by element 
int a[10];

for(i=0;i<10;i++)>
{
scanf("%d",&a[i]);
}

  • similarly we cant equalize two array by a single assignment ooperater we have to do it by writing a for loop

examples:

/*wap to read 10 numbers and print them using arrays*/

main()
{
int a[10],i;

printf("give the numbers");

for(i=0;i<10;i++)>
scanf("%d",&a[i]);


for(i=0;i<10;i++)>
printf("%d",&a[i]);

}

out put: 

give the numbers:
12345678910

12345678910

dryrun:


  • array is declared...
  • a printf statement is given 
  • a for loop is written to read the input
  • a forloop is writen ti print output.
  • once i=0 the loop is enterd and it prits ist number and once i=2 it prints 2nd number so on

Thursday, December 18, 2008

DAILY:2-CLASS PRACTICE

Class practice is a section similar to the renew section ...but what differs is this section deals with current section not the basic section and contains mostly the practice sessions held in the class...

1./*wap to get 

1
12
123
1234
12345
*/

main()
{
int i,j;

for(i=1;5>=i;i++)
{
for(j=1;5>=j;j++)
printf("%d",j);

printf("\n");
}


}


dry run:
  • analyse the pattern . .. . . .. .before writing the program
  • in the pattern the numbers are printed from 1 to 2,1 to 3 ..... 1-5 if generalized it's 1-n
  • the whole set is repeated 5 times such that n increases from 1-n in each repetition of above series
  • so the pattern is nothing but a repitition of 1-n in general which is inturn repeated n times.
  • so we cunstruct one loop to print 1-n and other to repaet this loop n times.

2./*
wap to print

5
54
543
5432
54321
*/

main()
{

int i,j;

for(i=5;i<=1;i--)
{

for(j=n;j>i;j--)
printf("%d",j);

printf("\n");
}


}

dryrun:
  • this prog is similar to the above program
  • here in the above pattern the numbers are repeated from n-1 in general
  • a loop is cunstructed in order to evaluate the pattern
  • this pattern is repeated n times and an according loop is cunstructed.
  • if i=1;i is less than5 and the loop is enterd if j=5 and j is more than thanor equal to i hence it prints frpm n-i or 5-1 so on .....all the two loops are executed .

3./* wap to print

12345
1234
123
12
1
*/

main()
{

int i,j;

for(i=5;i>0;i--)
{

for(j=1;j<=i;j++)
printf("%d",j);

printf("\n");

}





4./*wap to print 

5
45
345
2345
12345

*/

main()
{

int i,j;

for(i=1;i>0;)
{

for(j=i;j<=5;j++)
printf("%d",j);
printf("\n");
}


}

5./*

12345
2345
345
45
5

*/

main()
{

int i,j;

for(i=5;i>0;i++)
{

for(j=i;j<=5;j++)
printf("%d",j);
printf("\n");
}

}


6./*

54321
5432
543
54
5

*/

main()
{

int i,j;

for(i=5;i>0;i++)
{

for(j=5;j>=i;j--)
printf("%d",j);
printf("\n");

}



}

7./*

1
121
12321
1234321
123454321
*/

main()
{

int i,j,k;

for(i=1;i<=5;i++)

{

for(j=1;j<=i;j++)
printf("%d",j);

for(k=(i-1);k>0;k++)
printf("%d",k);

printf("\n");
}


}

8./*

123454321
1234321
12321
121
1
*/

main()
{

int i,j,k;

for(i=5;i>0;i--)
{

for(j=1;j<=i;j++)
printf("%d",j);

for(k=(i-1);k>0;k++)
printf("%d",k);

printf("\n");


}


}

9./*

5
545
54354
5432543
543215432
*/

main()
{

int i,j;

for(i=5;i<0;i--)>
{

for(j=i;j<=i;j--)
printf("%d",j);

for(k=5;k<=i-1;k--)
printf("%d",k);

printf("\n");


}




}

Tuesday, December 16, 2008

RENEW 1 :

RENEW is like an exensin to basic which gives a whole lot of practice in aready discussed basic posts..this gives a immense practice and workout schedule to 'RENEW' ones capablity in programming with 'c'

1./* wap which accepts the dimensions of a rectangle and displays its perimeter*/



main()
{

float l,b,p;
printf("give the dimensions");
scanf("%f %f",&l,&b);

p = 2*(l+b);

printf("the perimeter is %d",p);


}

dry run :

  • we declared 3 variables l,b,p corresponding to length, breadth and perimeter of the rectangle.
  • we requested user to provide the values of l,b
  • we saved respective values in thier variables l,b;
  • appliying formula perimeter = 2*(length+breadth)
  • printing final value of p
out put:
give the dimensions 5 5
the perimeter is 20

2. /* wap which accepts basic salary of a person , also his expenditure on rent(in percent of his salary),food(in percent of his salary) and other amenities(in percent of his salary) and dedects his gross savings*/


main()
{

float salrary,rent,food,other,saving;
printf("give salary,rent,food,other");
scanf("%f %f %f %f",&salary,&rent,&food,&other);

saving = salary - ((food+rent+other)/100)*salary);

printf("%d is savings",saving);

}

dry run:

  • create all convienient variables
  • take salary,rent,food,and other expenditure in to the respective variables
  • calculate savings by the above formula
  • print the savings
out put
give salary food rent and other - 10000 25 25 10 
4000 is the savings

3./*wap which accepts coefficients of a quadratic equation and prints its roots*/


#include(math.h)
main()
{

int a,b,c;
float r1,r2;
printf("give the coefficients");
scanf("%d %d %d",&a,&b,&c);

r1= -b+sqrt(b*b-4*a*c)/(2*a);
r2= -b-sqrt(b*b-4*a*c)/(2*a);

printf("%f %f",r1,r2);


}

dry run:
  • take integer variables for coefficients be cause they are generally not flaot type
  • take float type for roots as they are generally of float type
  • request values from user
  • read the values into respective variables
  • operate with the formulas and obtain the roots
  • print the roots
4./*wap which accepts coordinates of a point and determines its distance from the origin*/


#include(math.h)
main()
{
float x,y,d;
printf("give coordinates");
scanf("%f %f",&x,&y);

d= sqrt(x*x+y*y);

printf("%f",d);
}


5./*wap to find modulous of a given number*/

#include
main()
{

int x,a;
printf("give n");
scanf("%d",&n);

a=(x>0)?x:-x;

printf("|x| = %d",a);
}

output:

give n -6
|-6| = 6

dryrun:

  • take a variable and request a value
  • take the value into the variable
  • assume a variable that stores a value which is dependent on the condition x>0
  • if condition is true it stores x in it else -x here we use conditional operator.
  • print the resultant variable to get desired result

6./*wap to double a nuimber with out using any arthematic operators*/


main()
{

int a,b;
printf("give a");
scanf("%d",&a);

b=a<<1;>
printf("%d",b);


}

out put:
give n 6
12

dry run
  • the above prog is just an example for leftshift
  • we take a variable,reqest for a value...and read tht value into the variable..
  • then we operate a with leftshift one time .....and name the resultant as b
  • the bits written leniarly ....are shifted one pos to left and resultant is the double of the initial 
  • we at last print the resultant
  • this program is just an example for left shift it does not work with all numbers

7./*wap to demonstate increment and decrement operators*/


main()
{

int a,b,c,d,e,f,g;
printf("give a value of a");
scanf("%d",&a);

b =a++;
c=++a;
d=a++ + ++a
e=++a + a++
f =a++*++a
g=++a*a++

printf("%d %d %d %d %d %d %d ",a,b,c,d,e,f,g);
}



dry run + out put:

  • request the user and read in value of a
  • as per operation of an incerment operator b is a++ ,ie if a is five thaen b will become 5
  • at the end of the above step a will become 6 so as c=++a , first increment is done and then assignment which follows hence, c will become 6+1=7
  • at the end of the above step a is 7 so as d=a++ + ++a; first increment is done so a=7+1=++a,which is added to a,ie d =8+8=16;and at the end a=8+1=9
  • similarly by folloyin all the operation of increment and decrement operaors, we get all the above results

sources:

  • "let us c" by Yashwant Kanethkar
  • "C programming" and data structures by Balaguruswamy