Find frequency of occurrence, minmax without storing input - c

The task is to read N digits within a range from 0 to 9 and find frequency of occurrence for entered digits. In addition to that, I need to find min and max values and also the most common number(s). Using at least 3 functions is required(storing input isn't allowed))
So far I completed finding occurrences and min and max value entered.
I have problems with finding the most common number(s) and creating functions. I have created separate variables for each number to find the frequency.
Also, this code doesn't seem to be very good and in case you have better solutions for this problem, please share.
{
int n=0;
int max=0,min=9;
int freq0=0,freq1=0,freq2=0,freq3=0,freq4=0,freq5=0,freq6=0,freq7=0,freq8=0,freq9=0;
for (;;)
{
printf ("Please enter the number in range from 0 to 9 (to break enter: 11): ");
scanf ("%d",&n);
if (n==11)//to exit the loop 11 is setteled as a stop value
break;
if (n==0)
freq0++;
if (n==1)
freq1++;
if (n==2)
freq2++;
if (n==3)
freq3++;
if (n==4)
freq4++;
if (n==5)
freq5++;
if (n==6)
freq6++;
if (n==7)
freq7++;
if (n==8)
freq8++;
if (n==9)
freq9++;
if (n>max)
max=n;
if (n<min)
min=n;
}
printf ("Number 0 occured %d times\n",freq0);
printf ("Number 1 occured %d times\n",freq1);
printf ("Number 2 occured %d times\n",freq2);
printf ("Number 3 occured %d times\n",freq3);
printf ("Number 4 occured %d times\n",freq4);
printf ("Number 5 occured %d times\n",freq5);
printf ("Number 6 occured %d times\n",freq6);
printf ("Number 7 occured %d times\n",freq7);
printf ("Number 8 occured %d times\n",freq8);
printf ("Number 9 occured %d times\n",freq9);
printf ("highest %d \n", max);
printf ("smallest %d\n",min);
return 0;
}

#include <stdio.h>
#include <limits.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
int main(void) {
int n=0;
int value_max=INT_MIN;
int value_min=INT_MAX;
int freq[10] = {0};
for(;;)
{
printf ("Please enter the number in range from 0 to 9 (to break enter: 11): \n");
scanf ("%d",&n);
if (n==11) break;
freq[n]++;
value_max = MAX(n, value_max);
value_min = MIN(n, value_min);
}
for(n=0; n<10; ++n)
{
if (freq[n] == 0) continue;
printf("Frequency of %d : %d\n", n, freq[n]);
}
printf("Maximum value : %d\n", value_max);
printf("Minimum value : %d\n", value_min);
return 0;
}
Test Input
4 5 6 1 2 3 4 5 6 11
Output
Frequency of 1 : 1
Frequency of 2 : 1
Frequency of 3 : 1
Frequency of 4 : 2
Frequency of 5 : 2
Frequency of 6 : 2
Maximum value : 6
Minimum value : 1

Try this.
#include <stdio.h>
int min(int a, int b) { return (a < b) ? a : b; }
int max(int a, int b) { return (a > b) ? a : b; }
int valid(int n) { return (n >= 0 && n <= 9) ? 1 : 0; }
int main() {
int i, n, min_number = 10, max_number = -1, max_frequency = 0;
int freq[10] = {0,0,0,0,0,0,0,0,0,0};
// Read all numbers from input (read until end of file or press Ctrl+Z in console)
while (scanf("%d", &n) > 0) {
if (valid(n) == 0) {
printf("Number %d is invalid and will be ignored\n", n);
continue;
}
freq[n] += 1;
min_number = min(min_number, n);
max_number = max(max_number, n);
max_frequency = max(max_frequency, freq[n]);
}
printf("Occurences of digits:\n");
for (i = 0; i < 10; i++)
printf("Number %d occured %d times\n", i, freq[i]);
printf("\n\nMin value: %d\n", min);
printf("Max value: %d\n", max);
// Print numbers with greatest frequency
printf("\n\nMost common numbers: ");
for (i = 0; i < 10; i++) {
if (freq[i] == max_frequency)
printf("%d\n", i);
}
return 0;
}

#include <stdio.h>
void test(int N){
int arr[] = {0,0,0,0,0,0,0,0,0,0};
for (int i =0; i < N; i++){
int num;
printf ("Please enter the number in range from 0 to 9 (to break enter: 11): ");
scanf ("%d",&num);
if (num==11) break;
if (num>9 || num<0){
printf("Number is not in range 0..9! Try another number..\n");
i--;
continue;
}
arr[num]++;
}
int max = 0, min = 9, max_occurrences = 0, min_occurrences = N;
for (int i = 0; i < 10; i++){
printf ("Number %d occured %d times\n", i, arr[i]);
max_occurrences = arr[max_occurrences] < arr[i] && arr[i]!=0? i : max_occurrences;
min_occurrences = arr[min_occurrences] > arr[i] && arr[i]!=0? i : min_occurrences;
max = max < i && arr[i]!=0? i : max;
min = min > i && arr[i]!=0? i : min;
}
printf("max occurrences : %d \nmin occurrences : %d \nmax common : %d \nmin common : %d \n ", max_occurrences, min_occurrences, max, min);
}
int main()
{
test(6);
return 0;
}

Related

How to get sequence of numbers and then print the last 5?

Im trying to make a program that will get sequence from the user that end with 0, and then i want to print the last 5 numbers (not including the 0).
I can assume that the user will input all the numbers in one line and will end it with 0.
I wrote that code but something is wrong with it, I think its something about the scanf line.
Input:
1 6 9 5 2 1 4 3 0
Output: no output
#include <stdio.h>
#define N 5
int main()
{
int arr[N] = {0};
int last_input, j;
printf("please enter more than %d number and than enter 0: \n", N);
last_input = 0;
while (last_input<N) {
scanf(" %d", &j);
if (j == '0') {
last_input = N;
break;
}
else {
arr[last_input] = j;
}
if (last_input==(N-1)) {
last_input=-1;
}
++last_input;
}
printf("The last %d numbers u entered are:\n", N);
for (j=(last_input+1); j<N; ++j) {
printf(" %d", arr[j]);
}
for (j=0; j<last_input; ++j) {
printf(" %d", arr[j]);
}
return 0;
}
This comparison
if (j == '0') {
does not make a sense because the user will try to enter the integer value 0 instead of the value (for example ASCII 30h or EBCDIC F0h) for the character '0'.
You need to write at least
if (j == 0) {
Due to these sub-statements of the if statement
last_input = N;
break;
this for loop
for (j=(last_input+1); j<N; ++j) {
printf(" %d", arr[j]);
}
is never executed and does not make a sense.
This statement
last_input=-1;
results in breaking the order of the N last elements in its output. And moreover the result value of the variable last_input will be incorrect.
You need to move elements of the array one position left. For this purpose you can use a loop of standard C function memmove.
The program can look the following way.
#include <stdio.h>
#include <string.h>
int main( void )
{
enum { N = 5 };
int arr[N];
printf( "Please enter at least not less than %d numbers (0 - stop): ", N );
size_t count = 0;
for (int num; scanf( "%d", &num ) == 1 && num != 0; )
{
if (count != N)
{
arr[count++] = num;
}
else
{
memmove( arr, arr + 1, ( N - 1 ) * sizeof( int ) );
arr[N - 1] = num;
}
}
if (count != 0)
{
printf( "The last %zu numbers u entered are: ", count );
for (size_t i = 0; i < count; i++)
{
printf( "%d ", arr[i] );
}
putchar( '\n' );
}
else
{
puts( "There are no entered numbers." );
}
}
The program output might look like
Please enter at least not less than 5 numbers (0 - stop): 1 2 3 4 5 6 7 8 9 0
The last 5 numbers u entered are: 5 6 7 8 9
I made some changes based on ur comments and now its work fine!
#include <stdio.h>
#define N 5
int main()
{
int arr[N] = {0};
int last_input, j;
printf("please enter more than %d number and than enter 0: \n", N);
last_input = 0;
while (last_input<N) {
scanf("%d", &j);
if (j == 0) {
break;
}
else {
arr[last_input] = j;
}
if (last_input==(N-1)) {
last_input=-1;
}
++last_input;
}
printf("The last %d numbers u entered are:\n", N);
for (j=(last_input); j<N; ++j) {
printf("%d ", arr[j]);
}
for (j=0; j<last_input; ++j) {
printf("%d ", arr[j]);
}
return 0;
}
thank u guys <3.

Stop array from printing 0 at end

I made a guessing game where a random number is generated and the user has 10 tries to guess it. At the end of the game, the program prints the array of all tries. When the user guesses correctly in less than 10 tries, the array prints out all tries and prints zeros after so that there are 10 elements in total because of the for loop.
If the number is 62 and it's guessed in 6 tries for example it looks like
Your Tries: 50 90 60 65 63 62 0 0 0 0
I want to make it so that the extra zeros are not printed at the end of the game.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main (int argc, char** argv)
{
int s, n, v, win = 0, lose = 0;
char c;
srand( time(NULL) );
s = rand () % 100 + 1;
int tries = 0;
int a[n];
for (tries = 0; tries < 10; tries++)
{
scanf ("%d", &a[tries]);
if (a[tries] == s)
{
win++;
printf ("\nYou win!\n");
printf ("Your Tries: ");
for (tries = 0; tries < 10; tries++)
{
printf ("%d ", a[tries]);
}
printf ("\nTry Again? ");
scanf (" %c", &c);
if (c == 'n' || c == 'N')
{
printf("Your stats: %d Win, %d Lose", win, lose);
return 0;
}
if (c == 'y' || c == 'Y');
{
tries = 0;
s = rand () % 100 + 1;
scanf ("%d", &a[tries]);
}
}
printf ("The number is %s %d.\n", a[tries] > s ? "less than" : "greater than",
a[tries]);
}
printf ("You input wrong number. You lose. The number is %d.\n", s);
lose++;
printf ("Your Tries: ");
for (tries = 0; tries < 10; tries++)
printf ("%d ", a[tries]);
printf ("\nTry Again? ");
scanf (" %c", &c);
if (c == 'n'|| c == 'N')
{
printf("Your stats: %d Win, %d Lose", win, lose);
return 0;
}
if (c == 'y' || c == 'Y');
{
tries = 0;
s = rand () % 100 + 1;
scanf ("%d", &a[tries]);
}
}
It looks like you print out the last ten numbers using this:
for (tries = 0; tries < 10; tries++) {
printf ("%d ", a[tries]);
}
however it continues to loop even if you have less than ten numbers, a short fix could be to add a variable to store the number of tries, increase it every try and then use it in the for loop to not print too much numbers:
int tries = 0;
int nb_tries = 0;
int a[n];
for (tries = 0, nb_tries = 0; tries < 10; tries++, nb_tries++) {
scanf("%d", &a[tries]);
if (a[tries] == s) {
win++;
printf ("\nYou win!\n");
printf ("Your Tries: ");
for (tries = 0; tries < nb_tries; tries++) {
printf("%d", a[tries]);
}
}
}

C Program (Prime Number in a given range)

I have started learning C language. I wrote this program to find all prime numbers between the given range but I am unable to get the expected output.
Can anyone tell me what's wrong with this program please?
#include <stdio.h>
#include <conio.h>
void main() {
int min, max, i, j, count = 0;
printf("Enter Your First Number\n");
scanf("%d", &min);
printf("Enter Your Last Number\n");
scanf("%d", &max);
for(i=min; i<=max; i++) {
for(j=1; j<=i; j++) {
if(i % j == 0) {
count++;
}
}
if(count==2) {
printf("%d\t",i);
}
}
getch();
}
I just suggest getting rid of that count variable.
How do you know if a number N is prime? If for every j in the range (2 to N-1) you have N%j != 0.
So:
In the inner loop, use j from 2 to N-1 (instead of from 1 to N as you used tio do). In fact N%1 and N%N will be 0
The first time you find a j so that N % j == 0 break. You are sure it's not prime
Why incrementing count? For a prime number the j counter will be equal to i (because you looped until j<i, and the last j++ made j
equal to i). So just check for j == i and print the prime number i
#include <stdio.h>
#include <conio.h>
int main( void )
{
int min, max, i, j, count = 0;
printf("Enter Your First Number\n");
scanf("%d", &min);
printf("Enter Your Last Number\n");
scanf("%d", &max);
for(i=min; i<=max; i++)
{
// Was for(j=1; j<=i; j++)
for(j=2; j<i; j++)
{
if(i % j == 0)
{
//Was count++;
break;
}
}
//Was if(count==2)
if(j == i)
{
printf("%d\t",i);
}
}
getch();
return 0;
}
Here you are.
#include <stdio.h>
int main( void )
{
printf( "Enter the range of numbers (two unsigned integer numbers): " );
unsigned int first = 0, last = 0;
scanf( "%u %u", &first, &last );
if ( last < first )
{
unsigned int tmp = first;
first = last;
last = tmp;
}
do
{
int prime = first % 2 == 0 ? first == 2 : first != 1;
for ( unsigned int i = 3; prime && i <= first / i; i += 2 )
{
prime = first % i != 0;
}
if ( prime ) printf( "%u ", first );
} while ( first++ != last );
putchar( '\n' );
return 0;
}
The program output might look like
Enter the range of numbers (two unsigned integer numbers): 0 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
As for your program then you need re-initialize the variable count before the inner loop
for(i=min; i<=max; i++) {
count = 0;
for(j=1; j<=i; j++) {
if(i % j == 0) {
count++;
}
}
And the inner loop is inefficient.
Need to reset the value of count. It starts at count=0, then for any inputs, the loops will count up. The For each outer loop index, it will go like this:
1 (1%1=0 --> count++, count = 1)
2 (2%1=0 --> count++, and 2%2=0 --> count++, count = 3)
3 (3%1=0 --> count++, and 3%3=0 --> count++, count = 5)
etc... until max is reached.
You can use a simple isprime function to check whether a number is prime or not and then call the function for the given interval.
To find whether a number is prime or not , we can use a simple primality test to check it.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool isprime(int n)
{
if(n <= 1) return false;
if(n <= 3) return true;
if(n%2 == 0 || n%3 == 0) return false;
for(int i = 5;i*i <= n;i += 6)
{
if(n%i == 0 || n%(i + 2) == 0)
{
return false;
}
}
return true;
}
int main()
{
int a,b;
printf("Enter the first number :");
scanf("%d",&a);
printf("Enter the second number :");
scanf("%d",&b);
for(int i = a;i <= b;i++)
{
if(isprime(i)) printf("%d ",i);
}
return 0;
}
There is a simple change you should do:
#include <stdio.h>
#include <conio.h>
void main() {
int min, max, i, j, count;
printf("Enter Your First Number\n");
scanf("%d", &min);
printf("Enter Your Last Number\n");
scanf("%d", &max);
for(i=min; i<=max; i++)
{
count=1;
for(j=2; j<=i; j++)
{
if(i % j == 0) {
count++;
}
}
if(count==2) {
printf("%d\t",i);
}
}
}
My answer may be a bit late, but since it's the same issue, i'll write it here in case it helps someone else coming to this thread in the future.
My code is written from the POV of a beginner (No complex functions or data types are used) as this is a code that mostly they will get stuck on.
Working:
User inputs the range.
Using the for loop, each number in the range is sent to the isprime function which returns TRUE or FALSE after checking the condition for being a prime number.
if TRUE : program prints the number.
if FALSE : program skips the number using continue function.
#include<stdio.h>
int isprime(int num);
int main() {
int min, max;
printf("Input the low number: ");
scanf("%d", &min);
printf("Input the high number: ");
scanf("%d", &max);
for(int i = min; i<=max; i++) {
if(isprime(i) == 1) {
printf("%d ", i);
}
else if(isprime(i) == 0){
continue;
}
}
return 0;
}
int isprime(int num) {
int count = 0;
for(int i=2; i<=(num/2); i++) {
if(num % i == 0 ) {
count ++;
}
else{
continue;
}
}
if(count>0){
return 0;
}
else if (count == 0){
return 1;
}
}

C - print int with variable 0s [duplicate]

This question already has answers here:
Variable leading zeroes in C99 printf
(1 answer)
printf string, variable length item
(2 answers)
Closed 5 years ago.
I've written a programm for college where I print out all prime number twins between two numbers. (f.e. between 1 and 12000)
In my printing statement i've written %04d for 4 digits. But what i want to do is to make this variable. (I tried %0%dd, but this didnt work.) I dont want to do write just the max digit count of int. I count hte digits of the int with int count = floor(log10(abs(b))) + 1;
Heres my complete code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(int a);
void listOfPrimeNumberTwins(int a, int b);
typedef int twins[10000][2];
int main(){
int a;
int b;
printf("Check if prime:\nEnter number: ");
scanf(" %d", &a);
b = isPrime(a);
if (b == 0){
printf("No Prime!");
} else if (b == -1){
printf("Negative number!");
} else {
printf("Prime!");
}
printf("\nPrime Number Twins:\nEnter number 1: ");
scanf(" %d", &a);
printf("Enter number 2: ");
scanf(" %d", &b);
listOfPrimeNumberTwins(a,b);
}
int isPrime(int a){
int i;
int b = 0;
if (a == 1){
return 0;
}
if (a <= 0){
return -1;
}
for (i = 2; i < a; i++){
if (a % i == 0){
return 0;
}
}
return 1;
}
void listOfPrimeNumberTwins(int a, int b){
int count = floor(log10(abs(b))) + 1;
int i;
int j = 0;
twins c;
b -= 1;
for (i = a; i < b; i++){
if (i > 1 && isPrime(i) == 1 && isPrime(i + 2) == 1){
c[j][0] = i;
c[j][1] = i + 2;
j += 1;
}
}
if (j == 0){
printf("No Prime number twins between %d and %d!", a,b);
} else {
printf("Prime number twins between %d and %d:\n", a,b);
for (i = 0;i < j; i++){
printf("%04d\t<-->\t%04d\n", c[i][0],c[i][1]);
}
}
}
How can I achieve what I want? Or is it just impossible like I want it?

Random Number Generating Giving Same Number

When writing a program for class, i had to create a random number generator for part of it, and whenever i run it, the random number generator will only generate the same number, that being "293".
I have removed the code that generates the number and placed it in a new program by itself, and it properly generates different numbers. Is here something else in the code that is causing the output to be the same every time?
int main(){
/*generates random number*/
int min = 100;
int max = 999;
int num = rand()%((max+1)-min)+min;
printf("number is: %d\n",num);
int input;
/*takes apart the generated number*/
int digitThree = num % 10 /1;
int digitTwo = num % 100 / 10;
int digitOne = num % 1000 / 100;
printf("Today we will play a game, you take ten guesses of a 3 digit number, and I will tell you which one is a match or a hit\n");
for(int i = 1; i<11; i++){
printf("\nGuess #%d.\n", i);
printf("What is your guess?\n");
scanf("%d", &input);
int inputThree = input % 10 /1;
int inputTwo = input % 100 / 10;
int inputOne = input % 1000 / 100;
/*checks if number is a hit or a match*/
if (inputThree == digitThree )
printf("Numer %d is a match\n", inputThree);
if(inputThree == digitTwo)
printf("Number %d is a hit\n", inputThree);
if(inputThree == digitOne)
printf("Number %d is a hit\n", inputOne);
if(inputTwo == digitThree)
printf("Number %d is a hit\n", inputTwo);
if(inputTwo == digitTwo)
printf("Number %d is a match\n", inputTwo);
if(inputTwo == digitOne)
printf("Number %d is a hit\n", inputTwo);
if(inputOne == digitThree)
printf("Number %d is a hit\n", inputOne);
if(inputOne == digitTwo)
printf("Number %d is a hit\n", inputOne);
if(inputOne == digitOne)
printf("Number %d is a match\n", inputOne);
if(inputOne == digitOne && inputTwo == digitTwo && inputThree == digitThree){
printf("Congrats! You guessed the number %d correctly!\n", num);
return 0;
}
if(i == 10)
printf("Last Guess!!\n");
printf("\n");
}
You have to use srand() function:
#include <time.h>
int main(){
time_t t;
/* Initialization to get random differents numbers each time */
srand((unsigned) time(&t));
/*generates random number*/
int min = 100;
int max = 999;
int num = rand()%((max+1)-min)+min;
printf("number is: %d\n",num);
Link -> https://www.tutorialspoint.com/c_standard_library/c_function_srand.htm

Resources