I apologize if this question seems too vague, but I am a complete beginner and I am stuck on how to count instances of numbers from an array.
I have a function called create_hist() which has three inputs: an input double array for which the numbers range from 0 to 16, an input integer which indicates how many elements are in the array, and an output integer array of size 17. The goal is to count how many of each numbers are in the first array, and then assign that number to the correct index of the output array i.e:
If the input array contains {0,1,1,2,3,4,4} then the count will be 7, and the output array should be {1,2,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0}.
I am very new to C programming and I don't know where to start. My thoughts were that I could iterate through each element in the input array, and if the element was equal to zero, then add 1 to the first index of the output array and so on. I know this is wrong but I don't know another way. Could someone please guide me on how I could begin this? My first attempt is below.
int create_hist( double input_array[], int count, int hist[17] ) {
for ( int i = 0; i < count; i++ ) {
if ( input_array[i] == 0; ) {
// then add 1 to hist[0]
}
if ( input_array[i] == 1; ) {
// then add 1 to hist[1]
// etc.
}
}
You're not a million miles away from a good solution. If you already have count available to pass in, that's handy. Otherwise, you can replace it with:
sizeof(input_array)/sizeof(double)
If you know for sure the maximum value that can appear in input array, then the rest of the problem should be pretty easy.
Loop through each item in input_array, like you're already doing. Then increment the index of histwhich relates to the value of the current item, like so:
for(int i = 0; i<count; i++){
hist[input_array[i]]++;
}
This should give the output you're looking for. For future reference, this is called a count occurrence algorithm.
The function can look the following way
size_t create_hist( const double in[], size_t n, int out[] )
{
const size_t N = 17;
memset( out, 0, N * sizeof( *out ) );
for ( size_t i = 0; i < n; i++ ) ++out[( size_t )in[i]];
size_t m = 0;
for ( size_t i = 0; i < N; i++ )
{
if ( out[i] ) ++m;
}
return m;
}
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
#define SIZE 17
size_t create_hist( const double in[], size_t n, int out[] )
{
const size_t N = SIZE;
memset( out, 0, N * sizeof( *out ) );
for ( size_t i = 0; i < n; i++ ) ++out[( size_t )in[i]];
size_t m = 0;
for ( size_t i = 0; i < N; i++ )
{
if ( out[i] ) ++m;
}
return m;
}
int main(void)
{
double a[] = { 0, 1, 1, 2, 3, 4, 4 };
int b[SIZE];
size_t n = create_hist( a, sizeof( a ) / sizeof( *a ), b );
printf( "There are %zu unique elements\n", n );
for ( size_t i = 0; i < SIZE; i++ ) printf( "%d ", b[i] );
putchar( '\n' );
return 0;
}
Its output is
There are 5 unique elements
1 2 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0
Related
I need to filter some specific elements from an array. I write the code which work perfectly:
#include <stdio.h>
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
int main (int argc, char *argv[]) {
// our initial array
int x[] = {1,2,-3,4,5};
// initialize filtered_array with pointer
int *filtered;
int upper_bound = 4, lower_bound = 1, i, j=0, total = -1,result;
size_t n = NELEMS(x);
printf("size of the main array: %d\n",n);
// check which element satisfy the condition and count them
for (i=0;i<n;++i)
{
total = ((x[i] >= lower_bound) && (x[i] <= upper_bound)) ? total+1 : total;
};
// allocate array size for filtered array
filtered = (int*) calloc(total,sizeof(int));
for (i=0;i<n;++i)
{
// filter element from main array and store them in filtered array
result = ((x[i] >= lower_bound) && (x[i] <= upper_bound)) ? 1 : 0;
if(result) {
filtered[j] = x[i];
++j;
};
};
for (i = 0; i<total+1; ++i){
printf("%d ",filtered[i]);
};
return 0;
}
But can I avoid to create a new array like I used filtered and dynamically do this for the main array by some overwrite trick?
For starters your program is incorrect.
Firstly you need to include the header <stdlib.h>
#include <stdlib.h>
Secondly, initially total is set to -1
total = -1
So if the original array contains only one element that satisfies the condition then total will be equal to 0 due to this statement
total = ((x[i] >= lower_bound) && (x[i] <= upper_bound)) ? total+1 : total;
As a result this statement
filtered = (int*) calloc(total,sizeof(int));
is trying to allocate a memory with the size equal to 0. If the memory will be allocated (it is implementation defined) then you may not write anything in this memory. Otherwise the program will have undefined behavior.
In any case you are counting elements that satisfy the condition incorrectly and hence allocating a block of memory of an incorrect size.
Also there is no sense to insert a null statement after compound statements as you are doing
for (i=0;i<n;++i)
{
//...
};
^^^
Remove such redundant semicolons.
And you are using an incorrect conversion specifier in this call of printf.
printf("size of the main array: %d\n",n);
^^^
You have to write
printf("size of the main array: %zu\n",n);
^^^
As for your question
But can I avoid to create a new array like I used filtered and
dynamically do this for the main array by some overwrite trick?
then what you need is to change the array in place and to track the number of actual elements that satisfy the condition.
For example
int x[] = {1,2,-3,4,5};
// initialize filtered_array with pointer
size_t n = NELEMS(x);
int upper_bound = 4, lower_bound = 1;
printf( "size of the main array: %zu\n", n );
size_t m = 0;
for ( size_t i = 0; i < n; ++i )
{
if ( x[i] >= lower_bound) && x[i] <= upper_bound )
{
if ( m != i )
{
x[m] = x[i];
}
++m;
}
}
for ( size_t i = 0; i < m; ++i )
{
printf( "%d ", x[i] );
}
putchar( '\n' );
If you need to reallocate the original array according to the number of elements that satisfy the condition then the code can look for example the following way
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//...
size_t n = 5;
int *x = malloc( n * sizeof( int ) );
memcpy( x, ( int[] ){ 1, 2, -3, 4, 5 }, n * sizeof( int ) );
int upper_bound = 4, lower_bound = 1;
printf( "size of the main array: %zu\n", n );
size_t m = 0;
for ( size_t i = 0; i < n; ++i )
{
if ( x[i] >= lower_bound) && x[i] <= upper_bound )
{
if ( m != i )
{
x[m] = x[i];
}
++m;
}
}
if ( m != n )
{
int *tmp = realloc( x, m * sizeof( int ) );
if ( tmp != NULL )
{
x = tmp;
for ( size_t i = 0; i < m; ++i )
{
printf( "%d ", x[i] );
}
putchar( '\n' );
}
}
free( x );
What you are looking for is called modification (in this case filtering) in place.
That's even pretty simple:
int* filter(int* begin, int* end)
{
int* pos = begin; // start at the beginning of the array
for(; begin != end; ++begin)
{
if(*begin != 0) // your appropriate condition!
// this one filters out all zero values...
{
*pos++ = *begin; // copy current element to retain into
// first position not yet used
// (pos always points to one element past
// the last one retained!)
}
}
return pos; // as above, points to one past the last element retained
}
The return value is important to know how many elements remained.
If you prefer, you can instead write an index-based variant avoiding pointer arithmetics...
The basic idea about the filtering algorithm is that you just copy the elements towards the front, overwriting those values that either are filtered away or have already been copied.
Edit: Additional explanations:
begin and end pointers are to be passed as pointer to first element of the array and pointer to one past the array (typical C++ semantics when iterating over C++ STL containers, though these come with their own iterator types instead of pointers...).
So you'd call the function like:
int array[N];
int* newEnd = filter(array, array + sizeof(array)/sizeof(*array));
This is typical C++ semantics when iterating over STL containers (though these come with their specific iterator types instead of poitners).
If you don't like:
size_t filter(size_t length, int array[length]) // note: length as array subscript
// is ignored anyway...
{
int* pos = array;
int* begin = array;
int* end = array + length;
// now following the identical loop as above
return pos - array; // pointer difference; if pos points one past the last
// element you get the *number* of elements retained
}
About *pos++ = *begin: That's nothing special and any good C book should explain that nicely to you...
It copies the value begin points to to the address pos points to and increments pos afterwards.
An indexing loop doing the same might look as follows:
size_t pos = 0;
for(size_t i = 0; i < length; ++i)
{
array[pos++] = array[i]
}
return pos;
#include <stdio.h>
int removeduplicates(int arr[],int n){
int j=0;
int temp[15];
if(n==0 || n==1){
return n;
}
for(int i=0;i<n-1;i++){
if(arr[i]!=arr[i+1]){
temp[j++]=arr[i];
}
temp[j++]=arr[n-1];
}
for(int i=0;i<j;i++){
arr[i]=temp[i];
}
return j;
}
int main(){
int n;
int num[];
num[]= {1,2,3,3,4,4,5,5,5};
n= sizeof(num)/sizeof(num[0]);
n=removeduplicates(num,n);
printf("%d",n);
return 0;
}
Here in this question I was writing a code to remove duplicates from a sorted array. But I am getting the following error although I defined the array size and although I provided the array size.
main.c:36:9: error: array size missing in ‘num’
int num[];
^~~
main.c:37:9: error: expected expression before ‘]’ token
num[]= {1,2,3,3,4,4,5,5,5};
This code snippet
int num[];
num[]= {1,2,3,3,4,4,5,5,5};
is syntactically incorrect.
Instead write
int num[] = {1,2,3,3,4,4,5,5,5};
Also within the function this declaration with the magic number 15
int temp[15];
and this statement
temp[j++]=arr[n-1];
in the substatement of this for loop
for(int i=0;i<n-1;i++){
if(arr[i]!=arr[i+1]){
temp[j++]=arr[i];
}
temp[j++]=arr[n-1];
}
do not make a sense.
To remove duplicates there is no need to define an auxiliary array.
The function can be written for example the following way as shwon in the demonstrative program below.
#include <stdio.h>
size_t removeduplicates( int arr[], size_t n )
{
size_t m = 0;
for ( size_t i = 0; i != n; )
{
if ( m != i )
{
arr[m] = arr[i];
}
++m;
while ( ( ++i != n && arr[i] == arr[i-1] ) );
}
return m;
}
int main(void)
{
int num[]= { 1, 2, 3, 3, 4, 4, 5, 5, 5 };
const size_t N = sizeof( num ) / sizeof( *num );
for ( size_t i = 0; i != N; i++ )
{
printf( "%d ", num[i] );
}
putchar( '\n' );
size_t m = removeduplicates( num, N );
for ( size_t i = 0; i != m; i++ )
{
printf( "%d ", num[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3 3 4 4 5 5 5
1 2 3 4 5
I want to print the number of unique elements instead of show the elements For example show 4. Means we have 4 unique elements
#include<stdio.h>
#define max 100
int ifexists(int z[], int u, int v)
{
int i;
for (i=0; i<u;i++)
if (z[i]==v) return (1);
return (0);
}
void main()
{
int p[max], q[max];
int m;
int i,k;
k=0;
printf("Enter length of the array:");
scanf("%d",&m);
printf("Enter %d elements of the array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
q[0]=p[0];
k=1;
for (i=1;i<m;i++)
{
if(!ifexists(q,k,p[i]))
{
q[k]=p[i];
k++;
}
}
printf("\nThe unique elements in the array are:\n");
for(i = 0;i<k;i++)
printf("%d\n",q[i]);
}
https://onlinegdb.com/Bk3tvQMpw
Sort the array then iterate through the elements and print out if the current element is different than the last:
int cmpint(const void *a, const void *b) {
return *(int *) a) < *(int *) b :
-1 ?
(
*(int *) b) < *(int *) a ?
1 :
0
);
}
int main() {
/* ... */
qsort(p, m, sizeof(*p), cmpint);
int n = 0;
for(int i = 0; i < m; i++) {
if(!i || p[i-1] != p[i]) n++;
}
printf("Number of unique elements: %d\n", n);
}
where p is your now sorted array and length is m as per example code. As qsort is expected O(m *log(m)) so will this aglorithm. If you don't sort the array it will be O(m^2) due to m linear searches.
If I have understood the question correctly what you need is to count unique elements in an array using a function and without defining an auxiliary array. That is there is no need to output the unique elements themselves.
In this case the corresponding function can look the following way as it is shown in the demonstrative program below.
#include <stdio.h>
int is_unique( const int a[], size_t n, int value )
{
while ( n != 0 && a[ n - 1 ] != value ) --n;
return n == 0;
}
int main(void)
{
int a[] = { 1, 2, 3, 3, 2, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t count = 0;
for ( size_t i = 0; i < N; i++ )
{
count += is_unique( a, count, a[i] );
}
printf( "There are %zu unique elements in the array.\n", count );
return 0;
}
The program output is
There are 3 unique elements in the array.
If you do not want to define one more function to count unique elements in an array then just move the loop in the function shown in the above demonstrative program inside main.
Here you are.
#include <stdio.h>
int main(void)
{
int a[] = { 1, 2, 3, 3, 2, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t count = 0;
for ( size_t i = 0; i < N; i++ )
{
size_t j = i;
while ( j != 0 && a[j - 1] != a[i] ) --j;
count += j == 0;
}
printf( "There are %zu unique elements in the array.\n", count );
return 0;
}
The program output is the same as shown above that is
There are 3 unique elements in the array.
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
instead of
void main()
Say we have
int arr1[]={1,2,3,4,5};
int temp1[], temp2[];
and that we need to copy the first 3 members from arr1 to temp1, and the second 3 from arr1 to temp2, so that
temp1={1,2,3};
temp2={2,3,4};
and so on and so forth, how would you go on about doing it? I am trying to write a program to check if a user inputted number contains a palindrome in a length k (where k is also chosen by the user) and my idea was to store the number in an array, take the first 3 members, put them in an array, reverse them and put that in another array and then compare, but I am stuck on how to solve the problem I mentioned above, I tried something like:
void copyandReverse(int arr[], int copy[], int reverse[], int start, int length)
{
for(int i=0; i<length; i++)
{
copy[start+i]=arr[start+i];
reverse[start+i]=arr[start+length-i-1];
}
}
but it seems to only copy the first 3 elements, and reverse them.
PS: I don't think we're allowed to use string or dynamic memory allocation, it's given that the inputted number contains less then 10 digits, so I made the temporary arrays with a constant size 10.
There is no any need to create auxiliary arrays to check whether a given array is a palindrome.
This can be done much simpler.
Write a function like this
int is_palindrome( const unsigned int a[], size_t n )
{
size_t i = 0;
while (i < n / 2 && a[i] == a[n - i - 1]) ++i;
return i == n / 2;
}
then in main if you have an array like
unsigned int a[10];
and you need to check whether a sub-array with three elements is a palindrome then you can do this in a for loop
size_t k = 3;
for ( size_t i = 0; i <= 10 - k; i++ )
{
if ( is_palindrome( a + i, k ) )
{
printf( "The sub-array at position %zu:", i );
for ( size_t j = i; j < k + i; j++ ) printf( " %u", a[j] );
puts( " is a palindrome" );
}
}
as #stark mentioned, just iterate over the array with two indices.
Please see an example implementation below. You just need to evaluate the actual size from the userinput before you validate it.
The code below assumes 0 as the delimited. After gathering the actual user input size, we start a loop with index i and j. Looping until they have swapped positions.
If a mismatch exists, we return FALSE, otherwise TRUE.
#include <stdio.h>
#define TRUE 1
#define FALSE 0
size_t getUserInputLength(int *arr, size_t len)
{
for (size_t i = 0; i < len; i++)
{
if (arr[i] == 0)
{
return i;
}
}
return len;
}
int checkPalindrome(int *arr, size_t len)
{
size_t i, j;
for (i = 0, j = len - 1; i <= j; i++, j--)
{
printf("Current indices: [i: %ld, j: %ld]\n", i, j);
printf("Current values: [i: %d, j: %d]\n", arr[i], arr[j]);
if (arr[i] != arr[j])
{
return FALSE;
}
}
return TRUE;
}
int main()
{
// even number of user input
int evenInput[] = {1, 2, 3, 4, 3, 2, 1, 0, 0, 0};
size_t evenLen = getUserInputLength(evenInput, 10);
printf("Even number returned: %d\n", checkPalindrome(evenInput, evenLen));
// odd number of user input
int oddInput[] = {1, 2, 3, 2, 1, 0, 0, 0, 0, 0};
size_t oddLen = getUserInputLength(oddInput, 10);
printf("Odd number returned: %d\n", checkPalindrome(oddInput, oddLen));
return 0;
}
Hello i'm trying for about 2 hours to create a program which will remove even numbers from a dinamyc allocated array(with malloc)in c.Can somebody help me with some tips or create the code.
p.s. this is my first topic here, so feel free to give me some tips about how to correctly post a qustion.
Let's assume that you already allocated dynamically an array of n elements and initialized it.
In this case the function that removes elements with even values can look the following way
size_t remove_even( int *a, size_t n )
{
size_t m = 0;
for ( size_t i = 0; i < n; i++ )
{
if ( a[i] % 2 != 0 )
{
if ( i != m ) a[m] = a[i];
++m;
}
}
return m;
}
It can be called the following way
size_t m = remove_even( p, n );
for ( size_t i = 0; i < m; i++ ) printf( "%d ", a[i] );
printf( "\n" );
where p is the pointer to your dynamically allocated array of n elements.
The function actually removes nothing. It simply moves odd elements to the beginning of the array.
You can then use standard C function realloc to delete physically the removed elements.
For example
int *tmp = realloc( p, m * sizeof( int ) );
if ( tmp != NULL ) p = tmp;
Here is a demonstrative program
#include <stdlib.h>
#include <stdio.h>
size_t remove_even( int a[], size_t n )
{
size_t m = 0;
for ( size_t i = 0; i < n; i++ )
{
if ( a[i] % 2 != 0 )
{
if ( i != m ) a[m] = a[i];
++m;
}
}
return m;
}
#define N 10
int main( void )
{
int *a = malloc( N * sizeof( int ) );
for ( size_t i = 0; i < N; i++ ) a[i] = i;
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
printf( "\n" );
size_t m = remove_even( a, N );
int *tmp = realloc( a, m * sizeof( int ) );
if ( tmp != NULL ) a = tmp;
for ( size_t i = 0; i < m; i++ ) printf( "%d ", a[i] );
printf( "\n" );
free( a );
}
Its output is
0 1 2 3 4 5 6 7 8 9
1 3 5 7 9
There are some things which you need to check before you try to code something, but I see that there is no code which you showed use.
SO is not a tutorial site, so this means that you should show us some code which actually does compile and ask here if there are some problems with that code.
Any way until than, this code should give you an Idea about how to check if a Number is odd or even:
#include<stdio.h>
#include<stdlib.h>
int main(void){
int n;
printf("Enter an integer:> ");
if((scanf("%d", &n)) != 1){
printf("Error, Fix it!\n");
exit(1);
}
if (n%2 == 0){
printf("Even\n");
}else{
printf("Odd\n");
}
return 0;
}
The whole story here is not about checking if Numbers inside an Array are odd or even, is about to find a way to check if a number is odd or even and only then you should check if inside that array there are odd or even numbers. I hope you understand my point.
One easy way to remove even numbers from an array in C is to create a new array of all odd elements starting from 1 to the maximum element present in the original array and then compare the original array and odd elements array (and do intersection) and put it in another array with same elements present in both arrays.
Here is the program:
#include<stdio.h>
int main(){
int a[20],n,i,max,j,k=0,l=0;
printf("enter limit of array ");
scanf("%d",&n);
printf("enter the elements ");
for (i=0;i<n;i++){
scanf("%d",&a[i]);
}
max=a[0];
for (i=0;i<n;i++){
if (a[i]>max){
max=a[i];
}
}
int b[max],c[n],count=0;
for (j=2;j<=max;j=j+2){
c[k++]=j;
count++;
}
for (i=0;i<n;i++){
for (j=0;j<count;j++){
if (a[i]==c[j])
b[l++]=a[i];
}
}
for (i=0;i<count;i++){
printf("%d ",b[i]);
}
return 0;
}