Count the number of times a number appears in an array - c

I'm working on a small program that counts the number of times an integer appears in an array.
I managed to do this but there is one thing I can't overcome.
My code is:
#include <stdio.h>
int count_occur(int a[], int num_elements, int value);
void print_array(int a[], int num_elements);
void main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i<20; i++)
{
num_occ = count_occur(a, 20, a[i]);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i<num_elements; i++)
{
if (a[i] == value)
{
++count; /* it was found */
}
}
return(count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
My output is :
Array:
2 5 0 5 5 66 3 78 -4 -56 2 66 -4 -4 2 0 66 17 17 -4
The value 2 was found 3 times.
The value 5 was found 3 times.
The value 0 was found 2 times.
The value 5 was found 3 times.
The value 5 was found 3 times.
The value 66 was found 3 times.
The value 3 was found 1 times.
The value 78 was found 1 times.
The value -4 was found 4 times.
The value -56 was found 1 times.
The value 2 was found 3 times.
The value 66 was found 3 times.
The value -4 was found 4 times.
The value -4 was found 4 times.
The value 2 was found 3 times.
The value 0 was found 2 times.
The value 66 was found 3 times.
The value 17 was found 2 times.
The value 17 was found 2 times.
The value -4 was found 4 times.
How can I avoid double lines in the output?

You can use a parallel array, this example uses char[20] in order to save some space:
#include <stdio.h>
int count_occur(int a[], char exists[], int num_elements, int value);
void print_array(int a[], int num_elements);
int main(void) /* int main(void), please */
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char exists[20] = {0}; /* initialize all elements to 0 */
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i < 20; i++)
{
num_occ = count_occur(a, exists, 20, a[i]);
if (num_occ) {
exists[i] = 1; /* first time, set to 1 */
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}
int count_occur(int a[], char exists[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i < num_elements; i++)
{
if (a[i] == value)
{
if (exists[i] != 0) return 0;
++count; /* it was found */
}
}
return (count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
This method is faster, as it skips values already readed and starts iterating from i in count_ocurr:
#include <stdio.h>
int count_occur(int a[], char map[], int num_elements, int start);
void print_array(int a[], int num_elements);
int main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char map[20] = {0};
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i < 20; i++)
{
if (map[i] == 0) {
num_occ = count_occur(a, map, 20, i);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}
int count_occur(int a[], char map[], int num_elements, int start)
/* checks array a for number of occurrances of value */
{
int i, count = 0, value = a[start];
for (i = start; i < num_elements; i++)
{
if (a[i] == value)
{
map[i] = 1;
++count; /* it was found */
}
}
return (count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i< num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}

I would suggest only printing the statement if the current index is also the index of the first occurrence of the number in question.
Inside count_occur, you have the index of each match in i. If you pass in the i from main to count_occur, you can do something such as returning -1 if that value is greater than the i in count_occur. Then if you get that -1 in main, don't print.
In addition, your algorithm could be made faster. Instead of searching the array linearly every time, you can sort a copy of the array so that the search can be done efficiently. (Even if you use one array to index and the other to search, it'll be faster - and still return values in the same order.)

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int count_occur(int a[], int num_elements, int value, bool selected[]);
void print_array(int a[], int num_elements);
int main(void){
int a[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int size = sizeof(a)/sizeof(*a);
bool ba[size];
memset(ba, 0, sizeof ba);
int num_occ, i;
printf("\nArray:\n");
print_array(a, size);
for (i = 0; i<size; i++){
if(ba[i] == true) continue;//skip already count
num_occ = count_occur(a, 20, a[i], ba);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
int count_occur(int a[], int num_elements, int value, bool ba[]){
int i, count = 0;
for (i = 0; i<num_elements; i++){
if (a[i] == value){
ba[i] = true;
++count;
}
}
return count;
}
void print_array(int a[], int num_elements){
int i;
for (i = 0; i<num_elements; i++){
printf("%d ", a[i]);
}
printf("\n");
}
Little improvement
int count_occur(int a[], int num_elements, int index, bool selected[]);
num_occ = count_occur(a, 20, i, ba);
int count_occur(int a[], int num_elements, int index, bool ba[]){
int i, count = 0;
for (i = index; i<num_elements; i++){
if (a[i] == a[index]){
ba[i] = true;
++count;
}
}
return count;
}

#include<stdio.h>
#include<string.h>
int main()
{
int arr[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int arrSize = sizeof(arr)/sizeof(arr[0]);
int tracker[20];
int i,j,k=0,l=0,count,exists=0;
for (i=0;i<arrSize;i++)
printf("%d\t", arr[i]);
printf("\n");
memset(tracker, '$', 20);
for (i=0, j=i+1, count=1, l=0; i<arrSize; i++)
{
j=i+1;
count=1;
l=0;
while (l < arrSize)
{
if (arr[i] == tracker[l])
{
exists = 1;
break;
}
l++;
}
if (1 == exists)
{
exists = 0;
continue;
}
while (j < arrSize)
{
if (arr[i] == arr[j])
count++;
j++;
}
tracker[k] = arr[i];
k++;
printf("count of element %d is %d\n", arr[i], count);
}
}

very simple logic to count how many time a digit apper
#include<stdio.h>
int main()
{
int a,b,c,k[10];
int p[10]={0};
int bb[10]={0};
scanf("%d\n",&a);
for(b=0;b<a;b++)
{
scanf("%d",&k[b]);
}
for(b=a-1;b>0;b--)
{
for(c=b-1;c>=0;c--)
{
if((k[b]==k[c])&&(bb[c]==0))
{
p[b]=p[b]+1;
bb[c]=1;
}
}
}
for(c=0;c<a;c++)
{
if(p[c]!=0)
{
printf("%d is coming %d times\n",k[c],p[c]+1);
}
}
return 0;
}

In your function :
int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i<num_elements; i++)
{
if (a[i] == value)
{
++count; /* it was found */
a[i] = INFINITY; // you can typedef INFINITY with some big number out of your bound
}
}
return(count);
}
And in main() you can edit for loop:
for (i = 0; i<20; i++)
{
if(a[i] != INFINITY)
{
num_occ = count_occur(a, 20, a[i]);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}

It is possible to solve with two arrays
#include <stdio.h>
int main() {
int array[12] = {1,2,2,5,2,5,7,6,2,4,2,4};
int array2[100] = {0};
int indicator = 1;
int i = 0,j;
int index = 0;
int number_count;
for(int i = 0; i<12;i++) {
indicator = 1;
for(j = i+1;j<12;j++) {
if(array[i] == array[j]){
indicator = -1;
break;
}
}
if(indicator == 1){
array2[index] = array[i];
index++;
}
}
for(int k = 0; array2[k]; k++) {
number_count = 0;
for(int m = 0; m<12;m++){
if(array2[k] == array[m]){
number_count++;
}
}
printf("%d was found %d times...\n",array2[k],number_count);
}
return 0;
}

Related

Returning every s-th component of a vector abort problem in c

The task was to create a program in which you input a vector and it returnes every s-th component of the vector. For example, if x = (1, 2, 3, 4, 5, 6) and s = 2, the output is (1, 3, 5). But I get a zsh abort warning.
#include <stdio.h>
void sampleVector(double arr[], int n, int s){
int j = 0;
for (j=0; j<n; j++) {
arr[j] = 0;
printf("%d: ",j);
scanf("%lf",&arr[j]);
}
int i=1;
printf("%f,", arr[i]);
for (i=1; i<n; i++){
s=s*i;
printf("%f", arr[s]);
}
}
int main() {
int n;
scanf("%d", &n);
double arr[3]={0,0,0};
int s;
scanf("%d", &s);
sampleVector(arr, n, s);
}
This is my program so far!
void printfEveryNth(const int *array, size_t size, size_t n)
{
if(array && n)
{
for(size_t index = 0; index < size; index += n)
{
printf("%d ", array[index]);
}
printf("\n");
}
}
int main(void)
{
int array[] = {1, 2, 3, 4, 5, 6};
printfEveryNth(array, 6 , 2);
}
https://godbolt.org/z/e3Tsa8GKj

Remove negative numbers and sort an array

Good day everyone,
my task is to remove all negative numbers from an array, and shorten it (return the new length as the amount of positive numbers). I tried doing that by BubbleSort all negative number to the right, and new length would be (old length - number of swap). My code simply freezes up the system.
I would be grateful if you guys could help.
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int remove_negatives(int *array, int length) {
int *a;
int n = length;
a = &array[n - 1];
for (int i = 0; i <= n - 1; i++) {
while (array < a) {
if (*array < 0) {
swap(a, array);
a--;
array++;
length--;
}
}
}
printialn(array, n);
return length;
};
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
printiln(remove_negatives(a, l));
return 0;
}
The while loop never stops, that's probably the reason your code freezes.
Your code only changes the address when the if statement is true. Which the array in your main() will stuck on the second (a[1]) element. So if we change change the address when the if statement is false...
#include<stdio.h>
#include<stdlib.h>
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int remove_negatives(int *array, int length) {
int *a, *head;
int n = length;
head = array;
a = &array[n - 1];
for (int i = 0; i <= n - 1; i++) {
while (array < a) {
if (*array >= 0) {
array++;
continue;
}
swap(a, array);
a--;
array++;
length--;
}
}
for (int i=0; i<length; i++) {
printf("%d ", *head);
head++;
}
puts("");
return length;
}
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
remove_negatives(a, l);
return 0;
}
Now the while loop works, buts as #wovano said, the answer is still wrong. Your code isn't exactly a "bubble sort". you swap all the negative number to the end of the array and didn't actually sort the array.
So, let's start from the beginning.
To simplify the process, let bubble sort first, and then find the new array length.
#include<stdio.h>
#include<stdlib.h>
void swap(int *p, int *q) {
int h = *p;
*p = *q;
*q = h;
}
int bubble_sort(int *array, int length) {
int i, j;
// Bubble sort
for (i=0; i<length-1; i++) {
for (j=i+1; j<length; j++) {
if (array[i]<array[j]) swap(&array[i], &array[j]);
}
}
// Find new array length
for (i=length-1; i>=0; i--) {
if (array[i]>=0) break;
length--;
}
return length;
}
int main(void) {
int a[] = {-1, 2, 4, -8, 3, 7, -8, 9, 3};
int l = sizeof(a) / sizeof(a[0]);
l = bubble_sort(a, l);
for (int i=0; i<l; i++) printf("%d ", a[i]);
puts("");
return 0;
}

Find how many times the largest digit appears in an array

I have no idea what's wrong with the function goes by the name "int Count_largest_even". It's supposed to take the largest digit found in the given array (by the function "int find") and find how many times the digit appears in the array.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Count_largest_even(int size, int *array, int large);
void ArrayPrint(int a[], int size);
int find(int array[], int size);
int arr1[16] = { 2, 22, 1, 3, 24, 94, 93, 12, 12, 66666, 21, 24, 8888, 21, 2, 33 };
int main() {
int mount;
int even;
ArrayPrint(arr1,16);
even = find(arr1, 16);
mount = Count_largest_even(16, arr1, even);
printf("\n The biggest even digit is : %d\n %d", even,mount);
system("pause");
return 0;
}
int find(int array[], int size){
int i = 0, digit, edigit = 0;
for (i = 0; i<size; i++){
while (array[i]!=0)
{
digit = abs(array[i] % 10);
if (digit > edigit)//checking condition for large
{
if (digit % 2 == 0)
{
edigit = digit;
}
}
array[i] = array[i] / 10;
}
}
return edigit;
}
void ArrayPrint(int a[], int size)
{
int i;
for (i = 0; i<size; i++){
printf("%d\n", a[i]);
}
}
int Count_largest_even(int size, int *array, int large)
{
int i;
int count = 0, digit;
for (i = 0; i < size; i++){
while ((array[i]!=0))
{
digit = abs(array[i] % 10);
if (digit == large)
{
count++;
}
array[i] = array[i] / 10;
}
}
return count;
}
As Ian Abbott said, you should not modify the array inside your loop.
But you can also do this in a single pass - some pseudo code:
int count = 0;
int largest_digit = 0;
for each digit:
if(digit > largest_digit) {
largest_digit = digit;
count = 1;
}
else if(digit == largest_digit)
count++;

How to do this manually. How to get the output for the 3rd, 5th, & 7th call to Print_Array?

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void Print_Array(int values[], int length);
void swap(int values[], int i, int j);
void Move_Max(int values[], int max_index);
void Simple_Sort(int values[], int length);
int main() {
How do I use these numbers?
Is SIZE & length one and the same thing?
int my_vals[SIZE] = {83, 89, 94, 73, 11, 33, 25, 34, 73, 41};
Print_Array(my_vals, SIZE); //<- FIRST CALL TO PRINT
Simple_Sort(my_vals, SIZE);
system("PAUSE");
}
void Simple_Sort(int values[], int length) {
int i;
So here length would be 10?
I am starting from the end of my_vals? So would I be starting at 73? Since it's 10-1 = 9? So the 9th would be 73?
for (i = length - 1; i > 0; i--)
{
Move_Max(values, i);
Print_Array(values, SIZE);
}
}
void Move_Max(int values[], int max_index) {
int max, i, maxi;
max = values[0];
maxi = 0;
for (i = 1; i <= max_index; i++)
{
if (max < values[i])
{
max = values[i];
maxi = i;
}
}
swap(values, maxi, max_index);
}
void swap(int values[], int i, int j) {
int temp;
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
void Print_Array(int values[], int length) {
int i;
for ( i = 0; i < length; i++)
printf("%d", values[i]);
printf("\n");
}
When you declare an array, say you have declared int array A[4] so it means that an array A has length of 4 i.e. A[0] to A[3].
In your case, my_vals array is of 10 length i.e. my_vals[0] to my_vals[9]
my_vals[0] = 83
..
..
my_vals[9] = 41
In your for loop you are iterating from last i.e. from my_vals[9] i.e. 41
for (i = length - 1; i > 0; i--)
So the initial value of i will be 9.
But here you need to iterate till i = 0 i.e.
for (i = length - 1; i >= 0; i--)

Completing my addarray () formula

I apologize for my vagueness in advance-this is my first post and I can really use some help.
The assignment is as follows:
/* Write a function named addarray() that returns the sum of the
elements of an array of int values. Your functions should take two
parameters, the array and the number of elements in the array. Make
your function work with the following program; */
/* arraysum.c
*
* Synopsis - displays the value returned by the function addarray()
* with 2 different sets of parameters.
*
* Objective - To provide a test program for the addarray() function.
* Your answers should be 55 and 0.
*
*/
#include <stdio.h>
int addarray(int [], int, int);
void main() {
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int array2[4] = {0, 0, 0, 0};
printf("The sum of array1 = %d\n", addarray(array1, 0, 10));
printf("The sum of array2 = %d\n", addarray(array2, 0, 4));
}
This is my solution aid:
int addarray(int s[], int i, int n) {
int sum = 0;
for (i = 0; i < n; i++) {
sum += s[i];
}
return sum;
}
I cant seem to figure out how to get the proper result. Any help would be appreciated.
This is what I have completed so far:
#include <stdio.h>
int addarray(int array1[], int num_elements);
void print_array(int array1[], int num_elements);
void main(void)
{
int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum;
printf("\nArray:\n");
print_array(array1, 10);
sum = addarray(array1, 10);
printf("The sum is %d\n .", sum);
}
int addarray(int array1[], int num_elements)
{
int i, sum=0;
for (i=0; i<num_elements; i++)
{
sum = sum + array1[i];
}
return(sum);
}
void print_array(int array1[], int num_elements)
{
int i;
for(i=0; i<num_elements; i++)
{
printf("%d ", array1[i]);
}
printf("\n");
}
I cant figure out how to get a second array to be summed up.
Such as Array2.
int is a reserved word.you can't give a variable the name int.besides,the assignment says the function should take two parameters,not 3.check this :
#include <stdio.h>
int addarray(int arr[],int size);
void main() {
int array1[10] = {1,2,3,4,5,6,7,8,9,10};
int array2[4] = {0,0,0,0};
printf("The sum of array1 = %d\n", addarray(array1,10));
printf("The sum of array2 = %d\n", addarray(array2,4));
}
int addarray(int arr[],int size)
{
int sum = 0 , n ;
for( n = 0 ; n < size ; n++ )
{
sum += arr[n];
}
return sum;
}

Resources