A
A
Anthony_19982019-11-13 11:18:01
Arrays
Anthony_1998, 2019-11-13 11:18:01

How to work with arrays in C?

Please help with the task: If the elements located after the minimum are ordered in descending order, then find the sum of the elements located before the minimum and divide it by the maximum.
Here is my code:

#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int a[100];
int i, j, n, min, max, ind, sum;
double ch;
char flag = 0;
sum = 0;
printf("Vvedite razmer vectora n=");
scanf("%d",&n);
for (i=0; i<n; i++) 
{
  printf("a[%d]=",i);
  scanf("%d", &a[i]);
}
min = a[0];
ind = 0;
for (i = 0; i < n; i++)
if (min > a[i])
  {
    min = a[i];
    ind=i;	
  }
printf("\n Min element = %d", min);
printf("\n Index min elementa = %d \n", ind);
max = a[0];
for (i = 0; i < n; i++)
  if (max < a[i]) max = a[i];
printf("\n Max element = %d \n", max);
for(i=ind+2; i<n; i++)
  {
    if(a[i-1] < a[i])
      {
        flag = 1; 
        break;
      }
    if(!flag) sum = sum + a[i]; 				
  }
ch = min / max;
printf("\n Summa elementov do min = %d \n", sum);
printf("\n Chastnoe min / max = %d \n", ch);
for (i=0; i<n; i++)
  printf("%2d", a[i]);
}

Only I have the sum of elements up to the minimum counts incorrectly. And dividing the minimum element by the maximum counts incorrectly, 0 is always written there.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zolg, 2019-11-13
@Zolg

find the sum of the elements located up to the minimum
and where do you have it?
And dividing the minimum element by the maximum counts incorrectly, 0 is always written there
and what do you think the result of an integer division of a by b should be equal to, with b greater than a ?

A
Anthony_1998, 2019-11-13
@Anthony_1998

That's it, hang up, I've already done everything.
Here is the code if anyone needs it:

#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
int main()
{
int a[100];
int i, j, n, min, max, ind, sum;
float ch;
char flag;
printf("Vvedite razmer vectora n=");
scanf("%d",&n);
for (i=0; i<n; i++) 
{
  printf("a[%d]=",i);
  scanf("%d", &a[i]);
}
min = a[0];
max = min; 
ind = 0;
for (i = 0; i < n; i++)
  if (min > a[i])
    {
      min = a[i];
      ind=i;	
    }
  if (max < a[i]) max = a[i];
printf("\n Min element = %d", min);
printf("\n Index min elementa = %d \n", ind);
max = a[0];
for (i = 0; i < n; i++)
  if (max < a[i]) max = a[i];
  printf("\n Max element = %d \n", max);
flag = 0;
for(i=ind+2; i<n; i++)
  {
    if(a[i-1] < a[i])
      {
        flag = 1; 
        break;
      }				
  }
  sum = 0 ;
    if(flag==0) 
      for(i=0; i<ind; i++)
        sum = sum + a[i];
  ch = (float)min / max;
printf("\n Summa elementov do min = %d \n", sum);
printf("\n Chastnoe min / max = %f \n", ch);
for (i=0; i<n; i++)
  printf("%2d", a[i]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question