Removing duplicate entry from an array using hashmap - c

int main(array<System::String ^> ^args)
{
int arr[]={1,2,3,2,9,8,1,2,3,9};
int a[9];
int size = sizeof(arr)/sizeof(arr[0]);
int str[256]= {'\0'};
int i = 0;
for(i ; i < size ; i++)
{
if(str[arr[i]] == 0 )
str[arr[i]]= 1;
}
for( i = 0 ; i < size ; i++)
{
if( str[arr[i]] == 1)
{
a[i] = arr[i];
}
}
for(i=0 ; i < size ; i++)
{
printf("%d->",a[i]);
}
return 0;
}
still in the new array a,I am getting old data...not sure wats missing here...
Any help would be highly appreciated.
Thanks in advance.

Logical mistakes
the final array won't go till size
the indexing for a is wrong
Simply use one loop to achieve this, analyze following :
int i = 0,j=0;
for( ; i < size ; i++)
{
if(str[arr[i]] == 0 )
{
str[arr[i]]= 1;
a[j++] = arr[i];
}
}
Now iterate till j on final array a

Related

I couldn't handle to write a histogram

My aim is to generate a histogram for repeated numbers. The code works well until the frequency is bigger than 2.
I think I know what is wrong with the code (line 9) but I cannot find an algorithm to solve it. The problem that I have is when it writes the histogram, it separates and then gathers it again.
My Input:
5
5 6 6 6 7
Output:
6:2 6:2 6:3
but the output I need is
6:3
I kind of see the problem but I couldn't solve it.
#include <stdio.h>
int main(){
int array[25];
int i, j, num, count = 1;
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d", &array[i]);
for (j = 0; j < i ; j++) {
if (array [i] == array[j]) {
count++;
printf("%d:%d ", array[i], count);
}
}
array [i] = array[j];
count = 1;
}
return 0;
}
You are trying to count occurrences before all units have been accepted, which is not possible unless you maintain a separate counter for each value, which in turn is not practical if there is no restriction on the input value range or the range is large.
You need to have obtained all values before you can report any counts. Then for each value in the array, test if the value has occurred earlier, and if not, iterate the whole array to count occurrences:
#include <stdio.h>
#include <stdbool.h>
int main()
{
// Get number of values
int num = 0 ;
scanf("%d", &num);
// Get all values
int array[25];
for( int i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
// For each value in array...
for( int i = 0; i < num ; i++)
{
// Check value not already counted
bool counted = false ;
for( int j = 0; !counted && j < i; j++ )
{
counted = array[j] == array[i] ;
}
// If current value has not previously been counted...
if( !counted )
{
// Count occurnaces
int count = 0 ;
for( int j = 0; j < num; j++ )
{
if( array[j] == array[i] )
{
count++ ;
}
}
// Report
printf("%d:%d ", array[i], count);
}
}
return 0;
}
For your example input, the result is:
5
5 6 6 6 7
5:1 6:3 7:1
It is possible to merge the two inner loops performing the counted and count evaluation:
// Count occurrences of current value,
bool counted = false ;
int count = 0 ;
for( int j = 0; !counted && j < num; j++ )
{
if( array[j] == array[i] )
{
count++;
// Discard count if value occurs earlier - already counted
counted = j < i ;
}
}
// If current value has not previously been counted...
if( !counted )
{
// Report
printf("%d:%d ", array[i], count);
}
}

Unshuffled string is different from original string after shuffling and unshuffling

I'm actually implementing a shuffling function with an un-shuffling function. I'm using the Fisher-Yates algorithm with pseudo random number generator with a fixed seed. The unshuffled string doesn't look like the initial string, however.
I've checked if my random array is the same in the shuffling function and the un-shuffling function. The unshuffling function is the same as the shuffling function but in reverse.
Here is my shuffling function :
void shuffle(char * phrase)
{
int size_phrase = strlen(phrase);
srand(seed);
int * rdm_array = (int*)malloc(sizeof(int)*size_phrase);
int i;
for(i = 0; i < size_phrase; i++)
{
rdm_array[i] = rand()%size_phrase;
//printf("%d", rdm_array[i]);
}
//begin shuffle here
int j;
int k = 0;
for(j = size_phrase -1 ; j > 0 ; j-- , k++)
{
int rdm_nb = rdm_array[k];
char temp = phrase[j];
phrase[j] = phrase[rdm_nb];
phrase[rdm_nb] = temp;
}
free(rdm_array);
}
And here is my unshuffling function :
void unshuffle(char * phrase)
{
int size_phrase = strlen(phrase);
srand(seed);
int * rdm_array = (int*)malloc(sizeof(int)*size_phrase);
int i;
for(i = 0; i < size_phrase; i++)
{
rdm_array[i] = rand()%size_phrase;
//printf("%i", rdm_array[i]);
}
//On commence le mélange ici
int j;
int k = size_phrase-1;
for(j = 0 ; j < size_phrase ; j++ , k--)
{
int rdm_nb = rdm_array[k];
char temp = phrase[j];
phrase[j] = phrase[rdm_nb];
phrase[rdm_nb] = temp;
}
free(rdm_array);
}
And here is my Output :
It looks like it's missing one loop or something like that.
Add a printf to show what gets exchanged in both shuffle and unshuffle like this
/* after this line */
int rdm_nb = rdm_array[k];
/* insert debug output */
printf("%d <-> %d\n", j, rdm_nb);
You will see that in function shuffle variable j counts from size_phrase -1 to 1 while in unshuffle it counts from 0 to size_phrase -1.
Probably you should change the for loop in shuffle to
for(j = size_phrase -1 ; j >= 0 ; j-- , k++)

Basic help on arrays in C

I can't display correctly my arrays, I don't understand what the problem is.
Here is my code :
int increment = 1;
int para_1_courant = 10;
int para_2_courant = 4;
int para_1_min = 5;
int para_1_max = 10;
int para_2_min = 1;
int para_2_max = 4;
int tab_para_automate[2][2] = {{0}};
int tab_para_application[1][3] = {{0}};
tab_para_automate[0][0] = para_1_min;
tab_para_automate[0][1] = para_1_max;
tab_para_automate[1][0] = para_2_min;
tab_para_automate[1][1] = para_2_max;
printf("coucou1");
tab_para_application[0][0] = para_1_courant;
tab_para_application[0][1] = para_2_courant;
tab_para_application[0][2] = increment;
printf("coucou2\n");
int k,l;
for (k=0 ; k<1 ; k++)
{
for (l=0 ; l<1 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}
In output I only get this : "coucou1", "coucou2", 5
The loop:
for (k=0 ; k<1 ; k++)
goes until k < 1, so just for k = 0, and similarly it goes for l = 0, so you get just one iteration to display the element at tab_para_automate[0][0].
You should make both loops condition < 2 if you wnat to show all elements.
for (k=0 ; k<2 ; k++)
{
for (l=0 ; l<2 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}
You mistyped size of matrix
You should rewrite the code on this way to take care about index 1
for (k=0 ; k<=1 ; k++)
{
for (l=0 ; l<=1 ; l++)
{
printf("%d\n", tab_para_automate[k][l]);
}
}

simulation algorithm implementation in C/C++

Let a row of 8000 lamps. Initially, only the one located to the left is lit.
Then, every second, the following operation is performed: each lamp changes state (on or off) if the one on its left was lit a second before. The leftmost lamp stays on all the time. This operation is instantaneous.
The process stops when the lamp at the right end lights for the first time.
How many lights are on?
My following implementation of the problem is false, can you help me?
#include <cstdio>
int t[8001][2];
int main()
{
t[1][0] = 1;
t[1][1] = 1;
int cpt1 = 0, ip = 0;
while (t[8000][0] != 1 && t[8000][1] != 1)
{
ip++;
for (int j=2;j<8001;j++)
{
if(t[j-1][!(ip&1)])
t[j][(ip & 1)] = !t[j][!(ip & 1)];
}
}
for(int j = 1;j < 8001; j++)
cpt1 += t[j][1];
printf("cpt=%d\n", cpt1);
}
Code is missing an update when the left does not change.
Code simplified (zero based offset, use of bool) and corrected below
#include<stdbool.h>
#include<stdio.h>
#define N 8000
bool t[N][2];
int main(void) {
t[0][0] = true;
t[0][1] = true;
int ip = 0;
while (t[N - 1][0] == 0 && t[N - 1][1] == 0) {
ip = !ip;
for (int j = 1; j < N; j++) {
if (t[j - 1][!ip]) {
t[j][ip] = !t[j][!ip];
} else {
t[j][ip] = t[j][!ip]; // add
}
}
}
int cpt1 = 0;
for (int j = 0; j < N; j++) {
cpt1 += t[j][1];
}
printf("N=%d cpt=%d\n", N, cpt1);
return 0;
}
Output
N=8000 cpt=2048
the following proposed code:
cleanly compiles
uses C header files rather than C++ header files
performs the desired operation, but not the fastest possible algorithm
is liberally commented
And now the proposed code:
#include <stdio.h>
int t1[8000]; // initially all zeros
int t2[8000];
int main( void )
{
// setup initial conditions
int numLitLights = 0;
t1[0] = 1;
// while stop condition not true
while ( t1[7999] != 1 )
{
// make one pass through lamps
// update values
for (int j=0; j<7999; j++)
{
if( t1[j] )
{
t2[j+1] = ( t1[j+1] )? 0 : 1;
}
}
// update original
for( int j=0; j< 8000; j++ )
{
t1[j] = t2[j];
}
}
// count lit lamps
for(int j = 0; j < 8000; j++)
{
if( t1[j] )
{
numLitLights++;
}
}
// output number of lit lamps
printf( "number of lit lamps: %d\n", numLitLights );
} // end function: main
The result (number of lamps lit) is
1024

Dynamically Size Array in C

So If I have something that was Dynamic (IE iterated through a for loop) similar to this...
for (i=0; i <= SCREENWIDTH; i++)
{
}
And I wanted to create an array of size SCREENWIDTH and add entries to it. Is there a way I can do this?
so PSUEDO wise it would be...
int[SCREENWIDTH] e = {1,2,....SCREENWIDTH}
for (i=0; i <= SCREENWIDTH; i++)
{
e[i]= i;
}
You can do this like so:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int SCREENWIDTH = 80 ;
int *arr = (int *)malloc( sizeof(int) * SCREENWIDTH ) ;
if( NULL != arr )
{
for( int i = 0; i < SCREENWIDTH; ++i )
{
arr[i] = i ;
}
for( int i = 0; i < SCREENWIDTH; ++i )
{
printf( "%d, ", arr[i]) ;
}
printf("\n") ;
}
}
In C you can create dynamic array using malloc. Example in your case:
int * e = (int*)malloc(SCREENWIDTH*sizeof(int));
Once you allocate memory dynamically in this way. The next think you can do is initialization of the array using the loop.
There is a mistake the way you are accessing the loop. In C The indexing starts from 0 to n-1.
Example: In your case you can access only from e[0] to e[SCREENWIDTH-1].
So, please correct your loop by making it i < SCREENWIDTH. So, it will be
int *e = (int*)malloc(SCREENWIDTH*sizeof(int));
for (i=0; i < SCREENWIDTH; i++)
{
e[i]= i;
}

Resources