Today’s Agenda

1 / 11

# Today’s Agenda - PowerPoint PPT Presentation

Today’s Agenda. Recursion Array of Structures Introduction to Pointers. Recursion. Binary Search Power Series Maximum in an array Factorial using accumulator method Exercise Minimum in an array. Binary Search. int main() { int a[10] = {1,2,3,4,5,6,7,8,9,10}; int x=2; int index;

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Today’s Agenda' - everly

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Today’s Agenda

Recursion

Array of Structures

Introduction to Pointers

Recursion
• Binary Search
• Power Series
• Maximum in an array
• Factorial using accumulator method
• Exercise
• Minimum in an array
Binary Search

int main()

{

int a[10] = {1,2,3,4,5,6,7,8,9,10};

int x=2;

int index;

index = bSearch(a,0,9,x);

if(index == -1)

else

printf("number found at location %d\n",index);

}

intbSearch(int a[],intlow,inthigh,int x)

{

intmid,index;

mid = (low + high)/2;

if(low>high)

index = -1;

else if(x > a[mid]) {

low = mid+1;

index = bSearch(a,low,high,x);

}

else if(x < a[mid]) {

high = mid-1;

index = bSearch(a,low,high,x);

}

else if(x == a[mid])

index = mid;

return index;

}

Power Series (1 + x^2 + x^3 + …+ x^n)

int main()

{

int n=5,x=2;

int sum;

sum = recSum(x,n);

printf("Sum = %d\n",sum);

}

Power Series (1 + x^2 + x^3 + …+ x^n)

int recSum(int x,int n)

{

int ans;

if(n == 0)

ans = 1;

else

ans = pow(x,n) + recSum(x,n-1);

return ans;

}

Maximum in an array

int main()

{

int a[5] = {12,21,78,9,7};

int max;

max = maxm(a,5);

printf("max = %d\n",max);

}

Maximum in an array

intmaxm(int a[],int n)

{

int max;

if(n == 1)

max = a[n-1];

else

{

max = maxm(a,n-1);

if(max < a[n-1])

max = a[n-1];

}

return max;

}

Factorial using accumulator method

int main()

{

int n=5,acc=1,f;

f = fact(n,acc);

printf("%d! = %d\n",n,f);

}

int fact(intn,int acc)

{

if(n == 1)

return (acc*1);

else

return fact(n-1,n*acc);

}

Revisit Student Record

typedefstruct

{

intidno;

char name[20];

float marks[7];

discipline d; //declare enum for discipline

float percent;