Click here to Skip to main content
15,937,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>

int main()
{
   int i,a[5],b[5],c[5];
   float avg,max,min;

   for (i=0;i<5;i++)
   {
      printf("Enter the  value of first array : ");
      scanf("%d",&a[i]);
   }

   for (i=0;i<5;i++)
   {
      printf("\nEnter the  value of second array : ");
      scanf("%d",&b[i]);
   }

   for(i=0;i<5;i++)
   {
      c[i]=a[i]+b[i];
      printf("\n Sum of %d & %d = %d",a[i],b[i],c[i]);
   }

   {
      avg = c[i]/5.0 ;
      printf("\n Average = %.2f'", avg);
   }

   max = c[0];

   for(i=0;i<5;i++)
   {
      if(c[i]  >  max)
         max = c[i];
   }

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

   min = c[4];

   for(i=0;i<5;i++)
   {
      if (c[i]  <  min)
         min = c[i];
   }

   {
      printf("Min = %d\n", min);
   }

   return 0;
}


What I have tried:

I have tried to find out the max and min value from a sum of two arrays.
Posted
Updated 4-Mar-20 2:46am
v3

1 solution

First off, your Average won't work: You'd need to add each value together and then divide it by the number of values. Your code doesn't even use a loop, it just prinats the value of an element outside the bounds of your array of sums:
{
   avg = c[i]/5.0 ;
   printf("\n Average = %.2f'", avg);
}
It's possible that this prevents the code continuing - since c[5] is not a value array element, some systems will terminate your app with an error message when you try to use it.

What I'd strongly suggest is that you get familiar with 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.

[edit]
Oh, and stop throwing curly brackets in where they aren't needed, and leaving them out when they are a good idea.
I.e. less like this:
for(i=0;i<5;i++)
{
   if(c[i]  >  max)
      max = c[i];
}

{
   printf("Max = %d\n", max);
}
And more like this:
for(i=0;i<5;i++)
   {
   if(c[i]  >  max)
      {
      max = c[i];
      }
   }
printf("Max = %d\n", max);

[/edit]
 
Share this answer
 
v2

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