Question

a) Write a function that takes as input 2 parameters, an array and its size (both...

a) Write a function that takes as input 2 parameters, an array and its size (both ints) and returns the max. Include the driving (main program also. To pass an array to a function, do the following: return_type my_function(int my_array[], int size)

b) Write a function that does the above but can contain duplicate numbers and is capable of removing them. Scan in input. Note you may need the sizeof( ) function which is one of the standard libraries.Thus you must #include < > this library.

PLEASE WRITE THIS FOR PELLES C PROGRAMMING! THANK YOU

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

// Function to return max

int my_function(int my_array[],int size){
int max = my_array[0];

// Loop through the whole array
for (int i = 0; i < size; i++){
if(max<my_array[i]){
max = my_array[i];
}
}
return max;
}

int main()
{
int a[5] = {1,4,6,7,2};
int x = my_function(a,5);
printf("%d",x);
return 0;
}

Output: 7

b)

#include <stdio.h>

int my_function(int my_array[],int n){
int count = 0,i,j;
//To store array elements after removing duplicates

int a[n];

// Loop through the array
for (i = 0; i < n; i++)
{
for (j = 0; j < count; j++)
{
if(my_array[i] == a[j]) // If duplicate break
break;
}
if (j == count) // Else add it
{
a[count] = my_array[i];
count++;
}
}
printf("Array obtained after removing duplicate elements:\n");

for (i = 0; i < count; i++)
printf("%d\n", a[i]);
  
int max = a[0];
int size = sizeof(a)/sizeof(a[0]);
for (int i = 0; i < size; i++){
if(max<my_array[i]){
max = my_array[i];
}
}
return max;
}

int main()
{
int a[5] = {1,4,2,7,2};
int x = my_function(a,5);
printf("Max element : %d",x);
return 0;
}
Output:

Array obtained after removing duplicate elements:
1
4
2
7
Max Element : 7

Add a comment
Know the answer?
Add Answer to:
a) Write a function that takes as input 2 parameters, an array and its size (both...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • Write a function named numPerfect that takes as parameters: an array of integer scores between zero...

    Write a function named numPerfect that takes as parameters: an array of integer scores between zero and 100 (inclusive) the size of the array and returns the number of perfect scores in the array. Also write a main function that creates and initializes an array of ints (in the appropriate range), calls the function with that array, and prints out the return value.

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • Create a function that takes two parameters : pointer to the array and the size of...

    Create a function that takes two parameters : pointer to the array and the size of the array. Print out the array's values and their memory addresses inside the function. Then create another function that takes two parameters : pointer to the array. Inside the function, multiply each of the array's values by two ad print the modified values out in the main function. With C programming language

  • c++ please. Write a recursive method that takes the following parameters: * a sorted array of...

    c++ please. Write a recursive method that takes the following parameters: * a sorted array of ints * an int representing the size of the array * an int representing a value to be searched for Your function should return a bool. If the element is in the array, return true. Otherwise, return false. Your function should never produce memory access errors, regardless of the size of the array.

  • C Programming: Write a function that takes a size DIM array of double's as input and...

    C Programming: Write a function that takes a size DIM array of double's as input and sets every value in the array to zero. If not too much trouble, please annotate your work (so I'm learning and not just blindly copying), thankyou The exact info I was given is shown below. I have not idea what size DIM array means. arrayZero Function The first function you need to write this week is extremely simple. I takes a size DIM array...

  • C++ ProgrammingWrite a function that takes an integer array, the array size and a number....

    C++ ProgrammingWrite a function that takes an integer array, the array size and a number. And it determines how many times that number appears in the array. Just copy the following code and paste it to your answer. And fill the corresponding part. #include < iostream >using namespace atd; int howmany (int array lint , int number){ //Your code comes here} int main() {const int N =10; int array[N] = [11,3,2,1,3,4,6,9,1.3); int count = howlany(array,N,3);cout << 3 < "appesa" << cout << "times!"; return 0;

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT