A
A
Andrey Lyubimenko2018-11-21 16:06:41
C++ / C#
Andrey Lyubimenko, 2018-11-21 16:06:41

Why doesn't if in an array work as it should?

Task: Given two arrays of the same size, get the third array, each element of which is equal to the maximum of the elements with the same number in the given arrays? (sorry for the text in printf)

#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include <stdlib.h>
#include <time.h>

main()
{
  setlocale(LC_ALL,"RUS");
  int a[100],b[100],c[100];
  int i,n;
  
  printf("Vvedite razmer massiva: ");
  scanf("%d",&n);
  while(n<=1 || n>100)
  {
    printf("\nError! Povtorite : ");
    scanf("%d",&n);
  }
  for(i=0;i<n;i++)
  {
    a[i]=rand()%10;
    b[i]=rand()%15;
  }
  printf("\n1-massiv:\n ");
  for(i=0;i<n;i++)
  {
    printf("%d ",a[i]);
  }
  printf("\n2-massiv:\n ");
  for(i=0;i<n;i++)
  {
    printf("%d ",b[i]);  
  }
  printf("\nmassiv is max :\n ");
  for(i=0;i<n;i++)
  {
    if(a[i]=b[i])
    {
      c[i]=a[i];
    }
    else
    {
      if(a[i]>b[i])
      {
        c[i]=a[i];
      }
      else
      {
        c[i]=b[i];
      }
    }
  }
  for(i=0;i<n;i++)
  {
    printf("%d ",c[i]);
  }
  getch();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wexter, 2018-11-21
@andrewlybimenko

if(a[i]=b[i])
You either assign or compare

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question