Monday, June 16, 2014

Abstraction a simple programming strategy

It is necessary to have some formal way of constructing a program so that it can be built efficiently and reliably. Research has shown that this can be best done by decomposing a program into suitable small modules, which can themselves be written and tested before being incorporated into larger modules, which are in turn constructed and tested. The alternative is create what was often called "sphaghetti code" because of its tangled of statements and jumps. Many expensive, failed projects have demonstrated that, however much you like to eat sphaghetti, using it as a model for program construction is not a good idea!

It's rather obvious that if we split any task into a number of smaller tasks which can be completed individually, then the management of the larger task becomes easier. However, we need a formal basis for partitioning our large task into smaller ones. The notion of abstraction is extremely useful here. Abstractions are high level views of objects or functions which enable us to forget about the low level details and concentrate on the problem at hand.

To illustrate, a truck manufacturer uses a computer to control the engine operation - adjusting fuel and air flow to match the load. The computer is composed of a number of silicon chips, their interconnections and a program. These details are irrelevant to the manufacturer - the computer is a black box to which a host of sensors (for engines speed, accelerator pedal position, air temperature, etc) arconn.

In turn, the manager of a transport company has an even higher level or more abstract view of a truck. It's simply a means of transporting goods from point A to point B in the minimum time allowed by the road traffic laws. His specification contains statements like:

"The truck, when laden with 10 tonnes, shall need no more than 20l/100km of fuel when travelling at 110kph."


How this specification is achieved is irrelevant to him: it matters little whether there is a control computer or some mechanical engineer's dream of cams, rods, gears, etc.

There are two important forms of abstraction: functional abstraction and structural abstraction. In functional abstraction, we specify a function for a module, i.e.

"This module will sort the items in its input stream into ascending order based on an ordering rule for the items and place them on its output stream."


As we will see later, there are many ways to sort items - some more efficient than others. At this level, we are not concerned with how the sort is performed, but simply that the output is sorted according to our ordering rule.

The second type of abstraction - structural abstraction - is better known as object orientation. In this approach, we construct software models of the behaviour of real world items, i.e. our truck manufacturer, in analysing the performance of his vehicle, would employ a software model of the control computer. For him, this model is abstract - it could mimic the behaviour of the real computer by simply providing a behavioural model with program statements like:if ( pedal_pos > 50.0 ) { set_air_intake( 0.78*pedal_pos); set_fuel_valve( 0.12 + 0.32*pedal_pos); } Alternatively, his model could incorporate details of the computer and its program.

However, he isn't concerned: the computer is a "black box" to him and he's solely concerned with its external behaviour. To simplify the complexity of his own model(the vehicle as a whole), he doesn't want to concern himself with the internal workings of the control computer; he wants to assume that someone else has correctly constructed a reliable model of it for him.

Dynamic allocation

Dynamic allocatonThese variables are created when we need'em and then they must be deleted. 
notice that these variables are generated during the excution time not in the compiling time as the other variables.Creating a new variable : 
eg : int * xPtr = new int ; 
eg : char * NamePtr = new char[17] ;Deleting it : 
eg : delete xPtr; 
eg : delete [17] NamePtr ;

Friday, October 11, 2013

remove vowels string c

#include <stdio.h>
#include <string.h>
 
int check_vowel(char);
 
int main()
{
  char s[100], t[100];
  int i, j = 0;
 
  printf("Enter a string to delete vowels\n");
  gets(s);
 
  for(i = 0; s[i] != '\0'; i++) {
    if(check_vowel(s[i]) == 0) {       //not a vowel
      t[j] = s[i];
      j++;
    }
  }
 
  t[j] = '\0';
 
  strcpy(s, t);    //We are changing initial string
 
  printf("String after deleting vowels: %s\n", s);
 
  return 0;
}
 
 
int check_vowel(char c)
{
  switch(c) {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}

Thursday, October 10, 2013

Searching programs in c

Linear Search

#include <stdio.h>
 
int main()
{
   int array[100], search, c, number;
 
   printf("Enter the number of elements in array\n");
   scanf("%d",&number);
 
   printf("Enter %d numbers\n", number);
 
   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d",&search);
 
   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )     /* if required element found */
      {
         printf("%d is present at location %d.\n", search, c+1);
  break;
      }
   }
   if ( c == number )
      printf("%d is not present in array.\n", search);     
 
   return 0;
}
 

 Binary Search

#include <stdio.h>
 
int main()
{
   int c, first, last, middle, n, search, array[100];
 
   printf("Enter number of elements\n");
   scanf("%d",&n);
 
   printf("Enter %d integers\n", n);
 
   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter value to find\n");
   scanf("%d",&search);
 
   first = 0;
   last = n - 1;
   middle = (first+last)/2;
 
   while( first <= last )
   {
      if ( array[middle] < search )
         first = middle + 1;    
      else if ( array[middle] == search ) 
      {
         printf("%d found at location %d.\n", search, middle+1);
         break;
      }
      else
         last = middle - 1;
 
      middle = (first + last)/2;
   }
   if ( first > last )
      printf("Not found! %d is not present in the list.\n", search);
 
   return 0;   
}
 

Tuesday, September 10, 2013

Programming Problems(Find the output??)


What is the output of following programe?
Ques1.
#include <stdio.h>
main( )
{
i n t a, b y c;
scanf ( "%3d %3d %3d" , &a, &b, &c) ;
. . . . 

}
Input : 
1234 5678 9

Ans. Assignment will be like this:

a=123 b=4 c=567

Ques2.

Wednesday, September 4, 2013

Some interesting programs

#1 Printing the string containing Blank spaces while using Scanf

Problem: While using scanf for getting a string we have the limitation of string upto blank space.As soon as blank spaces occurs in a string scanf terminates to take that input.
 i.e .
char line[80];
scanf("%s",line);

for above program if we type
 India is my country. 
then line[ ] be assigned only "India"

Solution:
To solve this problem we could use a conversion process. 
Actually the "%s " in scanf converts our input to some string terminated with \0. As soon as the \0 occurs it does not take input. So following the rule of type conversion we can instruct our compiler to take input according to our own way.

An alternative method to print a string containing white space chararcter and uppercase character is that , write all uppercase letters including white sapce should be added in a square bracket in place of s, So that our compiler could interpret them and read them.

scanf (" %[  ABCDEFGHIJKLMNOPQRSTUWXYZ]", line ) ;

Now if our input is 
INDIA IS MY COUNTRY.

then line will be assigned the whole string "INDIA IS MY COUNTRY"
but now since we have not added the lowercase character hence as soon as it recognizes the lowercase letter it will terminate the scanning process.
Obviously , we could add lowercase character as well, but it will become cumbersome for us to write all those we want.


Now seeing the above solution, it comes to our mind that the above solution could be better if we could make compiler interpret the above things in opposite manner.i.e. what I mean to say that there should be some instruction , applying which we could help to ignore those characters which we are writing in code.
Because the ignored characters will be lesser than the used ones.

So ans is yes , we could use a circumflex (^)before our letters to be interpreted in opposite manner.

thus for ignoroing \n
the code will be simply like this :

scanf("%[^\n]",line);

Some C Facts

Scanf : In scanf all the operators are preceeded with the % sign because in scanf the argument presented by it are actually address. But the same does not apply to an array or string,why?
Because an array or string name is already an address of its first location , so no need to use % sign.

main( )
{char item[20];
int partno;
float cost;
scanf(" %s %d %f', item, &partno, &cost);
}