1.11M Members

Find the biggest element in that array

 
0
 

In the following program, implement the function
a. int *biggest(int *a, int count); This finds the largest element
in an array of integers.

#include <stdio.h>
#define SIZE 16
int* biggest(int *a, int count);
int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42,
10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n",
*p);
return 0;
}
int* biggest(int *a, int count)
{//[ADD YOUR CODE HERE]//
//I have done all the following but somithing must to be wrong!!!!//
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };  
   int i;
   for(i=0; i<SIZE; i++);{
     if((values[i]>=values[i+1]) && (values[i]>=values[i+2]) && (values[i]>=values[i+3]) && (values[i]>=values[i+4]) && (values[i]>=values[i+5]) && (values[i]>=values[i+6]) && (values[i]>=values[i+7]) && (values[i]>=values[i+8]) && (values[i]>=values[i+9]) && (values[i]>=values[i+10]) && (values[i]>=values[i+11]) && (values[i]>=values[i+12]) && (values[i]>=values[i+13]) && (values[i]>=values[i+14]) && (values[i]>=values[i+15]) && (values[i]>=values[i+16]))
    count = values[i];
    printf("values[%d]=%d\n",i,values[i]);
                       }
system("PAUSE");
}                          

}
 
0
 

Hmmm. Please use code tags. I'm not sure I understand what your rediculous if statement is doing...but I imagine it's supposed to be something like this:

int iBiggest(0);
for (int i(0); i < SIZE; ++i)
     if (values[i] > iBiggest)
            iBiggest = values[i]
return iBiggest;

that will iterate the loop and replace the iBiggest variable with the current element of the array, so long as it's larger than the last biggest number. It's good practice to avoid using constants when referencing an element of an array, unless there's a genuinely good reason to do so.

 
0
 
#include <stdio.h>
#define SIZE 16
int* biggest(int *a, int count);

int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n", *p);
return 0;
system("PAUSE");
}
int* biggest(int *a, int count)
{
     int iBiggest(0);
     for (int i(0); i < SIZE; ++i)
     if (values[i] > iBiggest)
     iBiggest = values[i]
     return iBiggest;
                       }
system("PAUSE");
}
 
0
 

Yeah that seems about right. Except get rid of the:
system("PAUSE");
}
at the end of the code.

 
0
 

#include <stdio.h>
#define SIZE 16
int* biggest(int *a, int count);

int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n", *p);
return 0;
system("PAUSE");
}
int* biggest(int *a, int count)
{

 
0
 

int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n", *p);
return 0;

}
int* biggest(int *a, int count)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int iBiggest = 0;
for(int i=0;i<SIZE;++i){
if (values > iBiggest)
iBiggest = values;
return iBiggest;
}


}

 
0
 

Yeah that's better. I don't understand what iCount is used for. And a redeclaration of the data inside the function seems redundant. Is it supposed to represent the size? I assumed size was a global int const, but if its not then you're going to have some problems. If it isnt, replace the SIZE in the for loop in the function with count. Do not redeclare the data inside the function. Instead, pass it in the form of int * a ( the int pointer argument) replace all values[] in the function with a[], and delete the initialization of the values[] array. This will pass in the data, making the function much more robust and usefull.

 
0
 

1) Get rid of the values array that is declared in biggest .... work with the array a that is passed instead (use a and count, rather than values and SIZE in the loop). Otherwise, biggest() will only ever find the largest value of that particular array.

2) You need to return a pointer to the largest element, not the index.

3) You've been advised to use code tags and have ignored that advice. Be aware, if you persist in not doing so, that people here will not be inclined to help you again. Look here for more information.

 
-1
 

#include <stdio.h>
#define SIZE 16
int* biggest(int *a, int count);
int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42,
10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n",
*p);
return 0;
}
int* biggest(int *a, int count)
{

 
0
 

Ummm....is that all the code? Please use code tags from now on. The syntax for code tags is [ code = cplusplus ] [ / code ] without any spaces. Wrap it around code like this:

cout << "This is code";

Also, add some meaningful text like a question or update/comment. Posting parts of code like this is not very productive use of anyones time.

 
0
 

int main(void)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int *p;
p = biggest(values, SIZE);
printf("the biggest element in the array is %i\n", *p);
return 0;

}
int* biggest(int *a, int count)
{
int values[16] = { 47, 17, 38, 91, 33, 24, 99, 35, 42, 10, 11, 43, 32, 97, 108, -8 };
int iBiggest = 0;
for(int i=0;i<SIZE;++i){
if (values > iBiggest)
iBiggest = values;
return iBiggest;
}


}

thanks

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article