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.