Home > Software design >  C Programming array with repetition
C Programming array with repetition

Time:07-27

I wrote a program that examines whether a number in a sequence is repeated 2 times (so many times it should be repeated) if it is repeated 2 times "YES", if not "NO". I think this can be written in a simpler and better way, so I'm interested in the suggestions and corrections of someone more experienced than me.
The program should print YES if each member of array is repeated exactly once (in total occurs twice in array), and otherwise it should print NO.



Enter the number of array: 6
Enter the sequence: 1 2 2 1 3 3
YES

Enter the number of string members: 5
Enter the string: 1 2 1 2 1
NO
#include <stdio.h>

#define arr 100
int main() {
  int n, i, p = 1, j, a;
  double array[arr];
  int num[100];
  do {
    printf("Enter the number of array: ");
    scanf("%d", & n);
  } while (n < 1 || n > 100);
  printf("Enter array: ");
  for (i = 0; i < n; i  ) {
    scanf("%lf", & array[i]);
    num[i] = -1;
  }
  for (i = 0; i < n; i  ) {
    a = 1;
    for (j = i   1; j < n; j  ) {
      if (array[i] == array[j]) {
        a  ;
        num[j] = 0;
      }

    }
    if (num[i] != 0)
      num[i] = a;

  }

  for (i = 0; i < n; i  ) {
    if (num[i] == 0)
      continue;
    if (num[i] != 2) {
      p = 0;
      break;
    }
  }
  if (p == 1)
    printf("YES");
  else
    printf("NO");
  return 0;
}

CodePudding user response:

I am going to provide a code review here because the code doesn't work and that makes it off-topic on Code Review.

Good things about the code:

  • There are no global variables.
  • There is an attempt to define a symbolic constant for the array size.

Things that can be improved:

  • Declare the variables are you need them. In the original version of C back in the 1970s and 1980s variables had to be declared at the top of the function. That is no longer the case, and a recommended programming practice to declare the variable as needed. In C the language doesn't provide a default initialization of the variable so variables should be initialized as part of the declaration. For readability and maintainability each variable should be declared and initialized on its own line.
  • Capitalize the constants, arr should be ARR.
  • ARR should be used in the declaration of num as well as the declaration of array.
  • ARR should be used instead of 100 in this statement while (n < 1 || n > 100).
  • Since the program only allows a single digit to be read at a time, array should be an array of integers rather than array of doubles.
  • There is only one error check on user input.

A simplified version of the program that does work:

One of the first things you need to learn in programming is how to write functions and subroutines. This is important because it allows you to break problems into smaller and smaller pieces until it is very easy to solve the problem. There are a number of programming principles that this relates to, 2 of them are the Single Responsibility Principle states:

that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by that module, class or function.

and the KISS Principle (Keep It Simple).

#include <stdio.h>
#include <stdbool.h>

#define ARR 100
static int get_user_input(int array[ARR])
{
    int count = 0;

    do {
        printf("Enter the number of array: ");
        scanf("%d", &count);
    } while (count < 1 || count > ARR);

    printf("Enter array: ");
    for (int i = 0; i < count; i  ) {
        scanf("", &array[i]);
    }

    return count;
}

static bool find_first_repetition(int count, int array[ARR])
{
    bool repetition = false;
    
    for (int i = 1; i < count; i  )
    {
        if (array[i - 1] == array[i])
        {
            return true;
        }
    }

    return repetition;
}

int main() {
    int n;
    int num[ARR];

    n = get_user_input(num);

    if (find_first_repetition(n, num))
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }

    return 0;
}  

  • Related