Tuesday, December 30, 2014

Programing Tricks ----Part1

Ques 1 We all know that a function cant return more than one value. So for modifying the original values we use pointers and thus can use the values accordingly in main. But if we do not want to use pointer then what will you use??

Answer-- we will use structure in that case

for example:-

 #include<stdio.h>
 struct marks{
     int maths;
     int physics;
     int chem;
    
 };
 struct marks deviation(struct marks student , struct marks student2 );
 int main(){
        
         struct marks student1;
         student1.maths= 87;
         student1.chem = 67;
         student1.physics=96;
        
         struct marks avg;
         avg.maths= 55;
         avg.chem = 45;
         avg.physics=34;
         //struct marks dev;
         struct marks dev= deviation(student1 , avg );
         printf("%d %d %d" ,dev.maths,dev.chem,dev.physics);

     return 0;
 }
struct marks deviation(struct marks student , struct marks student2 ){
     struct marks dev;
     dev.maths = student.maths-student2.maths;
     dev.chem = student.chem-student2.chem;
    dev.physics = student.physics-student2.physics;   
     return dev;
 }

Saturday, December 27, 2014

Programming basic questions Part-2

1. Can we define a function with arguments as pointer as well as value??

I mean if a swap funcation is defined as :-

void swap(int num1 ,int*num2 );

is it correct to define like this??

Ans :-

Yes, we can. there is no issue in this case but as we know that the function does not allow to return values more than one. thus doing this wont change the value of num1 in the calling function and our main objective of swapping will remain incomplete.

Thursday, August 7, 2014

Enumerated type(ENUMs)

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:


public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Monday, July 21, 2014

Print format string : printf

printf function as we know is used to give output on the screen,but while using so many times this function, I stuck at a point that why we use it in this way only ?


printf actually means print format string which helps us to print any variable whether it is integer float  character  or string. but what we need to do is wehave to tell this that we want to print particular type of variable in our statement.

printf undersatnds it and then converts that variable into string to give output as a statement.

for example:--
int num= 3;
float rate = 3.2;
char letter = 'A';

printf("%d, %f, %c" num, rate, letter);

output:--

3,3.2,A

for further information refer to this article.

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 ;