Find Quantity of Biggest integers in N integers in C - c

I want to do this code that tells you the number of (n) integers that are bigger (or equal) than a (k) input.
So for example:
input:
4 15
12
6
15
24
output:
2
So the 4 is the number of integers the user is going to input and the 15 is the k number, now the output is the quantity of numbers that are bigger than k.
What I have of code is this:
#include<stdio.h>
int main()
{
int n, k, i;
int c, d;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c[i]>k)
c[i]=d;
}
printf("%d", d);
return 0;
}
As you can see my code sucks, I don't know how to find the integers that are bigger than k and to print the quantity, not the numbers themselves. Any help is really appreaciated. Thanks.

Far less elegant solution, but one that keeps the value you need for some further use.. OldProgrammer did it much simpler and more pretty.
int main()
{
int num, s, i, cnt = 0;
printf("please input number of integers and int to compare with\n");
scanf("%d %d", &s, &num);
int arr[s];
for(i = 0; i < s; i++)
{
printf("Please input %d. number", i+1);
scanf("%d", &arr[i]);
}
for(i = 0; i < s; i++)
{
if(arr[i] >= num)
cnt++;
}
//at this point cnt holds the value you need
return 0;
}

Not sure why you are trying to reference c as an array. That is not needed. Try this:
int main()
{
int n, k, i, c;
int count = 0;
scanf(" %d",&n);
scanf("%d", &k);
for(i=1;i<=n;i++)
{
scanf("%d",&c);
if (c > k)
count++;
}
printf("%d", count);
return 0
}
Also, I would rename your variables to something more meaningful, such as numEntries, checkValue, etc.

Related

Can anyone notice where the mistakes is in my code?

Can anyone tell me why is my output wrong "1* 3 = 1" whereas other multiplications is correct? Thanks!
#include <stdio.h>
int main()
{
int n,i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n",n);
while(i<= 12)
{
printf("\n%d*%d=%d", i, n, total);
i++;
total = i*n;
}
}
You are printing a value, total, before you assign any value to it. On the first iteration of the while loop, total doesn't contain any defined value.
Here's a way to fix it:
#include <stdio.h>
int main(void)
{
int n, i = 1, total;
printf("\nPlease enter multiplication table: ");
scanf("%d", &n);
printf("%d Times Table\n", n);
while (i <= 12)
{
total= i * n;
printf("\n%d*%d=%d", i, n, total);
i++;
}
}
Simply calculate total before you print it, not after.

Finding the frequency of an element in an array using a user-defined function in C

I am writing a program where we need to find the frequency of the number chosen in an array. The numbers were inputted by the user and he/she will choose a number to count its instance. I did it with this program.
#include<stdio.h>
int main()
{
int n, frq=0, chn;
int num[10];
int i, j;
printf("Enter quantity of numbers to be inputted: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number[%d]: ", i+1);
scanf("%d", &num[i]);
}
printf("(");
for(j=0; j<n; j++)
{
printf("%d, ", num[j]);
}
printf(")\n\n");
printf("Enter number to be counted: ");
scanf("%d", &chn);
for(i=0; i<n; i++)
{
if(num[i]==chn)
{
frq++;
}}
printf("Instance count: %d", frq);
return 0;
}
It works perfectly fine. Then our teacher tasked us to do the same. But this time, we will use user-defined functions for the part where the program will count the instances of the chosen number. This is what I did.
#include<stdio.h>
int freq(int n);
int main()
{
int n, frq=0, chn;
int num[10];
int i, j;
printf("Enter quantity of numbers to be inputted: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number[%d]: ", i+1);
scanf("%d", &num[i]);
}
printf("(");
for(j=0; j<n; j++)
{
printf("%d, ", num[j]);
}
printf(")\n\n");
printf("Enter number to be counted: ");
scanf("%d", &chn);
frq = freq(n);
printf("Instance count: %d", frq);
return 0;
}
int freq(int n)
{
int chn, num[10], i, frq=0;
for(i=0; i<n; i++)
{
if(num[i]==chn)
{
frq++;
}}
return(frq);
}
But when I run it, it doesn't bring out the supposed output. For example;
input quantity: >>>6
Enter number[1]: >>>1
Enter number[2]: >>>2
Enter number[3]: >>>3
Enter number[4]: >>>3
Enter number[5]: >>>3
Enter number[6]: >>>2
(1, 2, 3, 3, 3, 2, )
Enter number to be counted: >>>3
Instance count: 1
Process Finished.
>>>
It was supposed to print 3 but instead, it prints 1. I don't know where I did wrong and I need your help guys. I really appreciate every answers and comments you put here, I run through every single one of them and it helped me. Thank you again!
ps. we were not allowed to use pointers or structures or idk in this program yet. Just in case some of you will suggest using them lol.
To write the function finding the frequency you need to pass:
The array
The size of the array
The number.
You simply declare the array inside the function thinking that it will magically have the content you have entered in another function. No, it contains undetermined values and invokes an UB
size_t freq(int *array, size_t size, int val)
{
size_t freq = 0;
while(size--) freq += *array++ == val;
return freq;
}
int main(void)
{
int num[] = {1, 2, 3, 3, 3, 2, };
printf("The %d is present in the array %zu times\n", 3, freq(num, sizeof(num)/sizeof(num[0]), 3));
}
or instead ( as #4386427 spotted) "oneliner"
while(size--)
{
if(*array == val) freq += 1;
array++;
}

Calculating Arithmetic and Geometric mean by introducing numbers until 0 is pressed

I have to calculate the arithmetic and geometrical mean of numbers entered by the user in C language. The algorithm works fine, but I don't know how to do the enter numbers until 0 is pressed part. I have tried many things but nothing works. Here is what I have tried to do until now. Thanks for the help.
int main() {
int n, i, m, j, arr[50], sum = 0, prod = 1;
printf("Enter numbers until you press number 0:");
scanf("%d",&n);
while (n != 0) {
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum = sum + arr[i];
prod = prod * arr[i];
}
}
int armean = sum / n;
float geomean = pow(prod, (float)1 / n);
printf("Arithmetic Mean = %d\n", armean);
printf("Geometric Mean = %f\n", geomean);
getch();
}
Your code is asking for the number of values in advance and subsequently reading that many values. That's not what you were asked to do.
You need to ask for numbers in a loop and exit the loop when the number that you read is 0. You don't even need an array:
int n = 0, i, m, j, sum=0, prod=1;
while (1) {
int value;
scanf("%d",&value);
if (value == 0) {
break;
}
sum=sum+value;
prod=prod*value;
n++;
}
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
You have to break the for loop when value 0 entered; so you should check for arr[i].
While loop is not required.
Please go through below code; this could be help full:
#include <stdio.h>
int main()
{
int n, i, m, j, arr[50], sum=0, prod=1;
printf("Enter numbers until you press number 0:");
for(i=0; i<50; i++)
{
scanf("%d",&arr[i]);
if (arr[i] == 0)
{
break;
}
sum=sum+arr[i];
prod=prod*arr[i];
}
printf ("%d %d\n",sum, prod);
n = i+1;
int armean=sum/n;
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
getch();
return 0;
}
what dbush said is right, you don't need array and are not asking the number in advance but what he did not tell is how can you find the number of values
int main()
{
int n, sum=0, prod=1, num;
printf("Enter numbers until you press number 0:\n");
for(n=0; ; n++)
{
scanf("%d",&num);
if(num==0)
break;
sum=sum+num;
prod=prod*num;
}
printf("sum is %d \n",sum);
printf("prod is %d \n",prod);
printf("n is %d \n",n);
float armean=sum/n; //why int?
float geomean=pow(prod,(float) 1/n);
printf("Arithmetic Mean = %d\n",armean);
printf("Geometric Mean = %f\n",geomean);
//getch(); why getch(), you are not using turboc are you?
}
There is no need for an array, but you should test if the number entered in 0 after reading it from the user. It would be better also to use floating point arithmetic to avoid arithmetic overflow, which would occur quickly on the product of values.
In any case, you must include <math.h> for pow to be correctly defined, you should test the return value of scanf() and avoid dividing by 0 if no numbers were entered before 0.
#include <stdio.h>
#include <math.h>
int main() {
int n = 0;
double value, sum = 0, product = 1;
printf("Enter numbers, end with 0: ");
while (scanf("%lf", &value) == 1 && value != 0) {
sum += value;
product *= value;
n++;
}
if (n > 0) {
printf("Arithmetic mean = %g\n", sum / n);
printf("Geometric mean = %g\n", pow(product, 1.0 / n));
getch();
}
return 0;
}

Output is the same for every number?

This program is supposed to asks the user to enter a positive integer (the integer could be of any number of digits in the range of the integer type) and replace each digit by the sum of that digit plus 6 modulus 10. The program then should swap the first digit with the last digit before it displays the output.
A sample input/output:
Enter the number of digits of the number: 5
Enter the number: 92828
Output: 48485
For some reason with my code, no matter what number I enter, everything just comes out as 6. (so if I enter 5 numbers, I get 666666). I'm new to pointers, so is there an issue with that, or do I just have some math wrong? The program runs without any compiler warnings.
#include <stdio.h>
#include <stdlib.h>
void replace(int *a, int *b, int n);
void swap(int *p, int *q);
int main()
{
int n = 0;
int i = 0;
int a[100], b[100];
//Prompt user to enter number of digits
printf("Enter the number of digits you'd like to replace: ");
scanf("%d", &n);
//Prompt user to enter the number to use
printf("Enter the number to use: ");
for(i = 0; i < n; i++);
scanf("%1d", &a[i]);
//replace function
replace(a, b, n);
for(i = 0; i < n; i++)
printf("%d", b[i]);
printf("\n\n");
return 0;
}
void replace(int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++)
{
*(b+i) = (*(a+i)+ 6) % 10;
}
printf("The output is: ");
//swap function
swap(b, (b+ (n-1)));
}
void swap(int *p, int *q)
{
int t;
t = *p;
*p = *q;
*q = t;
}
Your code is absolutely correct except a silly mistake in the following code snippet.
for(i = 0; i < n; i++);
scanf("%1d", &a[i]);
Why did you put a ; after the for statement? It means your for loop is just iterating once (instead of 5 if n = 5). As a result, only the first digit input is considered given by the user but that too be stored in a[5] (considering n = 5), values stored in a[0] to a[4] are all garbage value.
Just remove the semicolon and update your code as follows.
for(i = 0; i < n; i++)
scanf("%1d", &a[i]);
Now it works fine.
The culprit in your code is the semicolon after the for loop:
for(i = 0; i < n; i++)**;**
scanf("%1d", &a[i]);
So the scanf that you wrote is basically out of the for-loop and stores the first digit into a[n].

Program to print N first factorial numbers in c

Factorial number is a number that is multiplied by it's previous numbers. For example it's 5. 1*2*3*4*5 is it's factorial number.
I already made a program which prints factorial of any number, but i don't know how to make it to print N first factorial number in c.
For example i type 10. It must show first 10 numbers along with their factorials (Making a table)
Here is what i was made to print factorial of any number.Is there any possibility to do with while/ if else statements/ and for loop?
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
printf("Factorial of %d js %d\n", n, fakt);
getch();
}
You probably want this:
Program:
#include <stdio.h>
int main()
{
int i, n, fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i=1;i<= n;i++) //use braces to write more than one statement inside the loop
{
fakt=fakt*i;
printf("Factorial of %d is %d\n", i, fakt);
}
getch();
}
Output:
Enter a number:
5
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
#include <conio.h>
#include <stdio.h>
void main()
{
int f=1,i,v;
clrscr();
printf("Enter the number :");
scanf("%d",&v);
for(i=1;i<=v;i++)
{
f=f*i;
printf("num =%d and fac=%d\n",i,f);
}
getch();
}
this code will work
That code already uses the for loop. The while loop equivalent is:
i = 1;
while (i <= n) {
fakt = fakt*i;
i++;
}
#include <stdio.h>
int factorial(int n)
{
int i,fakt = 1;
printf("Enter a number:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fakt = fakt*i;
return fakt;
}
int main()
{
int n;
printf("Enter a number:\n");
scanf("%d", &n);
int i = 0;
for(i=1;i<=n;i++)
{
printf("Factorial for %d is %d\n",i,factorial(i));
}
return 0;
}
I think this will do the job just fine.
You can do a nested loop.
Run the parent loop from 1 to n,
and the nested loop will be your already working for loop.
You may want this:
#include <stdio.h>
int main()
{
int n, i, num, factorial;
printf("Enter the number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
num = i;
factorial = 1;
while(num)
factorial *= num--;
printf("%d \t %d\n", i, factorial);
}
return 0;
}
Output:
Enter the number of terms: 10
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Use this fastest version of factorial using recursion with if ..else statement
#include<stdio.h>
int fact(int n);
void main()
{
int n;
printf("\nEnter an integer:");
scanf("%d",&n);
fact(n);
}
int fact(int n)
{
int a;
if(n==0)
{
printf("The Factorial of 0 is 1\n");
return 1;
}
else
{
a=n*fact(n-1);
printf("The Factorial of %d is %d\n",n,a);
return a;
}
}
#include<stdio.h>
int main(int n){
int fact;
clrscr();
printf("Enter a number and type exit:\n");
scanf("%d",&n);
if(n!=0){
fact=n*main(n-1);
printf("Factorial of %d is %d\n",n,fact);
getch();
return fact;
}
else{
printf("Factorial of 0 is 1.\n");
getch();
return 1;
}
}

Resources