Friday, July 31, 2009

C++ Question?

This is part of an exam review that is supposed to help us study, but I do not know how to solve this problem. How do I start, what is the logic behind it?





Write a C++ function with a 1D


integer array as its input parameter, that returns both the largest and the


smallest number in that array. Also show the corresponding function call.


[Hint: You can either a. pass extra variables by reference and return the min, max values by modifying these


variables in the function, or b. pass a pointer to a structure, to the function.]

C++ Question?
Following is the program





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int ar[10],i;


clrscr();


void getminmax(int[]);


cout%26lt;%26lt;"\n Enter the elements";


for(i=1;i%26lt;=5;i++)


{


cin%26gt;%26gt;ar[i];


}


getminmax(ar);





getch();


}





void getminmax(int x[])


{


int i,j,k,t;





for(i=0;i%26lt;=5;i++)


{


for(j=i+1;j%26lt;=5;j++)


{


if(x[i]%26gt;x[j])


{


t=x[i];


x[i]=x[j];


x[j]=t;


}


}


}


cout%26lt;%26lt;"\n Largest Number is"%26lt;%26lt;x[4];


cout%26lt;%26lt;"\n Smallest Number is"%26lt;%26lt;x[0];





}








Don't wait for Ready made things, modify above program according to your needs.


--------------------------------------...





for more programs visit my blog at


http://codesbyshariq.blogspot.com/
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


int max, min;


void funmaxmin(int []);


void main()


{


int ar[10],i;


for(i=0;i%26lt;10;i++)


scanf("%d",%26amp;ar[i]);


funmaxmin(a);


getch();


}


void funmaxmin(int a[])


{


int i;


max= a[0];


min = a[0];


for(i=1;i%26lt;10;i++)


{


if(max%26lt;a[i])


max = a[i];


if(min%26gt;a[i])


min= a[i];


}


printf("Max = %d /n Min = %d",max,min);


}
Reply:#include %26lt;stdio.h%26gt;


int main()


{


int temp[10],i,tempmax,tempmin;


char ok;


tempmax=0;


tempmin=999999999;


for (i=0;i%26lt;=9;i++)


{


printf("Enter number%d: ",i);


scanf_s("%d",%26amp;temp[i]);


if (temp[i]%26lt;=tempmin) tempmin=temp[i];


if (temp[i]=%26gt;tempmax) tempmax=temp[i];


}


printf("Min:%d\n", tempmin);


printf("Max:%d\n", tempmax);


printf("Is it ok?(press Y to exit");


scanf_s("%s",%26amp;ok);


if (ok=='Y' || ok=='y') return 0;
Reply:the answers 3.3 best of luck





xxxxxxx


No comments:

Post a Comment