Question:
C programming question: find repeting numbers?
?
2010-04-26 15:01:50 UTC
my current code is and the next step is: im asked to print "bingo" when 5 of the 15 numbers scanned have the same value... i know a way but is too long, i have to put 225 "if" & "else if" to do it LOL.. any ideas for shorter code... thank you :)

#include
#include

#define MAX_STACK_SIZE 15

struct stack
{
int currentStackSize;
double currentStackItems[ MAX_STACK_SIZE ];
};

struct stack numbers; /* Stack currently in use by program */

void push( double ); /* Adds a number to the top of the stack */
double pop(); /* Removes a number from the top of the stack */




int main( void )
{
double input;
int counter = 0;

do
{
scanf( "%lf", &input );
if (input>0) push( input );
else if (input==0) pop();
else
break;
counter++;
}
while( counter < 15 );

printf( "Done reading\n" );

counter = 0;
do
{
printf( "Saved: %.2lf\n", pop() );
counter++;
} while ( counter < 15 );
}



void push( double input )
{
if ( numbers.currentStackSize >= MAX_STACK_SIZE ) return;

numbers.currentStackItems[ numbers.currentStackSize ] = input;
numbers.currentStackSize++;
}

double pop()
{
if ( numbers.currentStackSize < 1 ) return 0.0;

numbers.currentStackSize--;
return numbers.currentStackItems[ numbers.currentStackSize ];
}
Four answers:
ʃοχειλ
2010-04-27 07:24:46 UTC
/* This is a demo. I slightly modified your code. */

/* By the way, your code looks great! Congratulations! */



#include

#include



#define MAX_STACK_SIZE 15

#define MAX_FREQ_SIZE 1024



struct stack

{

int currentStackSize;

double currentStackItems[ MAX_STACK_SIZE ];

};



struct stack numbers; /* Stack currently in use by program */



void push( double ); /* Adds a number to the top of the stack */

double pop(); /* Removes a number from the top of the stack */



void init_freq ();

void add_freq (double);

void sub_freq (double);

int find_value (double);

int find_bingo ();





struct frequency_map

{

double value;

int count;

};



struct frequency_map frequency [MAX_FREQ_SIZE];

int freq_size = 0;





int main( void )

{

double input;

int counter = 0;

double temp;



int n;

int bingo;



init_freq();



do

{

scanf ("%lf", &input);

if (input > 0) {

push( input );

add_freq (input);

}

else if (input == 0) {

temp = pop();

sub_freq (temp);

counter--;

}

else

break;

counter++;

}

while( counter < 15 );



printf( "Done reading\n" );



counter = 0;

do

{

printf( "Saved: %.2lf\n", pop() );

counter++;

} while ( counter < 15 );



for (n = 0; n < freq_size; n++) {

printf ("%lf (%d)\n", frequency [n].value, frequency [n].count);

}



bingo = find_bingo ();



if (bingo >= 0)

{

printf ("---- Bingo: %lf\n", frequency [bingo]);

}

}







void push( double input )

{

if ( numbers.currentStackSize >= MAX_STACK_SIZE )

return;



numbers.currentStackItems[ numbers.currentStackSize ] = input;

numbers.currentStackSize++;

}



double pop()

{

if ( numbers.currentStackSize < 1 )

return 0.0;



numbers.currentStackSize--;

return numbers.currentStackItems[ numbers.currentStackSize ];

}



void init_freq ()

{

int i;



for (i = 0; i < MAX_FREQ_SIZE; i++)

{

frequency [i].value = 0.0;

frequency [i].count = 0;

}

}



void add_freq (double value)

{

int index;



index = find_value (value);



if(index >= 0) {

frequency [index].count++;

}

else {

frequency [freq_size].value = value;

frequency [freq_size].count = 1;

freq_size++;

}

}



void sub_freq (double value)

{

int index;



index = find_value (value);



if(index >= 0) {

frequency [index].count--;

}

else {

fprintf (stderr, "*** Invalid state: value not found: %lf\n", value);

exit (1);

}

}



int find_value (double value)

{

int i;



for (i = 0; i < freq_size; i++)

{

if (frequency[i].value == value)

return i;

}



return -1;

}



int find_bingo ()

{

int i;



for (i = 0; i < freq_size; i++)

{

if (frequency[i].count >= 5)

return i;

}



return -1;

}
Ratchetr
2010-04-26 15:33:19 UTC
You don't really need to store the numbers as you read them, just the count of how many times a number occurred. Assuming standard bingo rules where the numbers are 1 to 75, you need an array to hold 75 numbers. It might be easier to make it hold 76 numbers, since arrays start at 0, but bingo numbers start at 1. (If your numbers aren't 1 to 75, you'll have to adjust the size of the array accordingly).



Initialize this array to all 0's before you read the file.



When you read in a number, use the input as an index into the array. Increment the count at that location. Then check the count. If it's 5, you can print Bingo.
?
2017-01-21 10:39:55 UTC
we are going to use a for loop, and save each and every top numbers into an array. we are going to initiate the counter from 3, and increment each and every loop with 2. it is because of the fact 2 is the only top quantity it incredibly is even, so we are able to bypass all even numbers after 2. for each loop, we've an inner loop which rotates by all our saved top numbers, and we divide the counter with those numbers. If a minimum of between the numbers is divisible, then we proceed with the subsequent quantity. Else, we push into our top quantity array. After the loop has been carried out, the array will incorporate our top numbers. here is the code under. int MAX = 1000; int primes[MAX] = [2]; int c, p, x, flag; x=a million; for ( c=3; c<=MAX; c += 2 ) { flag=a million; for ( p=0; p < x; p++ ) { if ( c % primes[p] == 0 ) { flag=0; destroy; } } if ( flag == a million ) { primes[x] = c; x++; } } // show all the top numbers for ( int i=0; i
Sayee
2010-04-26 15:20:42 UTC
store the numbers in array (or list)

then try something like

num=0;

while(num
loop array[0] to array[max] { //(loop through the whole array)

if(array[num] == value)

counter++;

}

if(counter>=5) //

you have more than 5 occurences!

break from while loop



num++;

}

Hope it helps..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...