Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey, you! Let’s make a family! Oh wait, sorry, that came out wrong, I meant through math. If I were to give you an array, I want you to be able to tell who the father and son is. The father is the largest sum possible, while the son is the smallest sum possible. Both are identified using only 3 elements from the array.

Input

The first line contains the size of the integer array.
The succeeding lines contain the values inside the array.

Constraints:
- size >= 3
- each element = -2147483648

What I have tried:

#include <stdio.h>
void main()
{
    int arraysize;
    scanf("%d", &arraysize);
    int arr[arraysize];
    int sum;
    int largest;
    int smallest;

    for(int i=0; i<arraysize; i++)
    {
        scanf("%d",&arr[i]);
    }

    for(int i=0; i<arraysize; i++)
    {
    for(int n=i+1; n<arraysize; n++)
      {
        if(arr[i]>arr[n])
        {
            sum = arr[i];
            arr[i]=arr[n];
            arr[n]=sum;
        }
      }
    }

    for(int i=0; i<arraysize; i++)
    {
        smallest += arr[i];
        largest += arr[arraysize - i - 1];
    }
    printf("%d\n", largest);
    printf("%d", smallest);   
}
Posted
Updated 19-Nov-22 22:47pm
Comments
Graeme_Grant 19-Nov-22 19:56pm    
Homework and assignments are fun. What is the issue that you are having?

What values do your largest and smallest sums start with? Do you give them a value? Why not? If you don't, what do they contain when you try to add to them?

And why is your summation loop looking at all elements? How many doe sthe assignment need to you look at?

To be honest, a minute or so with the debugger would have shown you exactly what the problem is: get used to running your code in the debugger for testing and it'll save you huge amount of time.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
-The request each element = -2147483648 is not understandable.

-The function main() usually returns int, void is not provided by the standard.

-Uninitialized variables "smallest" and "largest" are used.

-The data is sorted first, which is a good idea. However, when calculating the sum you add everything together which leads to the fact that largest and smallest have the same values. The task requires that the smallest possible or the largest possible sum is formed from 3 elements from the field.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900