Reputation: 45
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, and otherwise it should print NO
:
Enter the number of array: 8
Enter the sequence: 1 2 2 1 3 3 4 4
YES
Enter the number of string members: 7
Enter the string: 1 2 1 2 1 3 4
NO
Here is my code:
#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;
}
Upvotes: 0
Views: 365
Reputation: 144951
Your code works, but there are some issues:
you should test for conversion failures in scanf()
: the return value must be 1
as there is a single conversion for each scanf()
call.
the counting loops are too complicated: just counting the number of occurrences with 2 nested loops is much simpler and has comparable time complexity, and using a += (array[i] == array[j]);
may produce fast branchless code on most architectures.
it is good style to finish the program output with a newline.
Here is a modified version:
#include <stdio.h>
int main() {
int n;
do {
printf("Enter the length of the array: ");
if (scanf("%d", &n) != 1)
return 1;
} while (n < 1 || n > 100);
double array[n];
printf("Enter array: ");
for (int i = 0; i < n; i++) {
if (scanf("%lf", &array[i]) != 1)
return 1;
}
int p = 1;
for (int i = 0; i < n; i++) {
int a = 0;
for (int j = 0; j < n; j++) {
a += (array[i] == array[j]);
}
if (a != 2) {
p = 0;
break;
}
}
if (p == 1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
Upvotes: 2
Reputation: 545
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:
Things that can be improved:
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)
.array
should be an array of integers rather than array of doubles.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("%1d", &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;
}
Upvotes: 2