Answer the question
In order to leave comments, you need to log in
How to sort an array so that it works (C language)?
Good evening! I ran into a problem, I don’t understand what needs to be done to make the array work. Thanks for the help!
Task code:
#include <stdio.h>
#include <stdlib.h>
#define N 16
int main() {
int A, i;
int a[] = {5,5,6,6,10,22,22,11,11,53,-3,-3,7,9,7,41};
int count=0;
printf("Input A="); scanf ("%i", &A);
printf("Выходные данные ");
for (i=0; i<N; i++) {
printf("%d ", a[i]);
}
printf("\nОдинаковые числа "); // нужно вывести одинаковые элементы массива, которые больше А
for (i=0; i<N; i++) {
if (a[i] == a[i-1] && (i+1== N || a[i+1] != a[i]))
{
count++;
printf("%i ", a[i]);
}
}
printf("\nКоличество = %d", count); //подсчитать количество одинаковых элементов массива, которые больше А
}
Answer the question
In order to leave comments, you need to log in
It would be better if you indicated in the question what specific problems, what outputs, etc.
On a whim: in this line, the array is out of bounds twice:
First time at i=0 in a[i-1], second time at i = N - 1: a[i+1].
Apparently you need to process the first and last element of the array separately (not in a general loop).
And in general, it seems to me that you are not solving the problem correctly.
if (a[i] == a[i-1] && (i+1== N || a[i+1] != a[i]))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question