This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to create a program that will count the frequency of 10 numbers that i will choose but when i am trying to run it it doesn't even run the printf and closes.Any ideas? thanks
#include <stdio.h>
int main()
{
int i,j,A[10]={0},C[10]={0};
for(i=0;i<10;i++)
{
scanf("%d /n",&A[i]);
}
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
getch();
}
The line
if(A[i]==j)
looks wrong - i was the counter for a previous loop and is now 10 (so beyond the bounds of your array). Did you mean
if(A[j]==j)
// ^
instead?
Changing this makes the program run for me. I don't think it does what you want yet. The break statement causes execution to halt the first time you find any match.
Hopefully this is enough hints to allow you to investigate how to count frequency of the numbers then print them all out yourself.
You need to again loop over the A array in order to check the value of each element:
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
if(A[i]==j)
{
C[j]=C[j]+1;
printf(" %d ",C[j]);
break;
}
}
}
If you know that the values in A will range from 0-9 then you could completely remove the inner loop and just do C[A[i]]++;
After this your C array will contain a count of each number the user input. e.g. C[5] will contain the number of 5s found, so you can output this as you see fit
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Problem : Rewrite small numbers from input to output. Stop processing
input after reading in the number 42. All numbers at input are
integers of one or two digits.
Example
Input : 1 2 88 42 99
Output : 1 2 88
My solution :
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
while(i!=42)
{
printf("%d",i);
scanf("%d",&i);
}
return 0;
}
Correct Solution :
#include <stdio.h>
int main( void ) {
int i;
while( 1 ) {
scanf( "%d", &i );
if( i == 42 ) break;
printf( "%d\n", i );
}
return 0;
}
Both the programs end if the input is 42, then what is the difference between the two?
EDIT : I just realized that this should have been posted at codereview.stackexchange.com Admins please do the needful.
Not too much. The only differences I see are that:
You don't output a newline "\n" after printing each number.
Your code isn't completely "DRY" (you repeat the line scanf("%d",&i);).
Otherwise they're (functionally) identical. But I feel compelled to add:
Your code is slightly uglier... Use proper indentation!
In the first program you enter the cycle after the user has entered the first value, so you have to check the value in the while condition.
The second program, instead, checks input always inside the cycle, and is therefore clearer.
From the question itself I can say that it is from one of the programming contest website practice questions.
These sites also match the format in which you are answering your question. So a newline(\n) is necessary.
They are functionally equivalent, and personally I much prefer your solution: while(1) is considered by some, including me, to be bad practice. It is easier to see when the while loop terminates just by looking at the single line, rather than having to trace through the code. A few differences otherwise:
You need to indent your code, or it is hard to read
You are not printing a newline (\n)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
This code is meant to list the number from 1-9 and display next to each number the frequency that it appears in the 10,000 random numbers that have been generated.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main()
{
int counts[10];
int i;
int random;
srand(time(0));
for (i = 0; i < 10; i++)
counts[i] = 0;
for (i = 0; i<= 10000; i++)
{
random = (int)(10.0*rand()/(RAND_MAX+1.0));
counts[random]++;
}
for (i = 0; i<10; i++);
printf("%d occurs %f%%\n", i, i/100.0);
return(0);
}
However when I run the program I get a list from 1-9 which I want but next to it lists 1-9 but in decimal instead of the frequency. this is what i get when i run it:
1 occurs 0.010000%
2 occurs 0.020000%
3 occurs 0.030000%
4 occurs 0.040000%
5 occurs 0.050000%
6 occurs 0.060000%
7 occurs 0.070000%
8 occurs 0.080000%
9 occurs 0.090000%
What do i need to change in order to get the frequency of each number,
also when I run the code from terminal using emacs it lists 1-9 but when I run the code in eclipse it only displays 10 occurs 0.100000% in the console. Is there any reason why this happens?
It looks like you're storing the frequencies of each number correctly, you're just not displaying them. Your output doesn't refer to the counts array at all.
In this line:
printf("%d occurs %f%%\n", i, i/100.0);
i/100.0 should be counts[i]/10000.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
How does the following code work?
void main()
{
printf("%d", printf("earth"));
}
This gives as output: earth5.
The return value of printf is the number of characters printed. The inner printf is called first. Equivalent to:
int rc = printf("earth");
printf("%d", rc);
This is absolutely fine :-)
The print("earth") outputs earth and return 5 (the number of characters printed).
The other printf gets the 5 as a parameter and outputs it as an integer (because of the %d)
%d is expecting an integer to print it. printf returns the number of printed chars, and you're printing a 5 char string.
It evaluates first the inner print to find out how many character were printed and then it evaluates the outer one printing 5.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have written a code to find nth ugly number(a number which has at least one prime factor grater than 5) where n is a given input. my program runs well if the user inputs something less than 240. But if input gets bigger than that, program crashes!. My question is if it is a time consuming problem then it should take time but why the program crashes? I have used double everywhere so it might not be the matter of variable capacity!!
my code is below:
#include<stdio.h>
#include<math.h>
double primes[1000]={2,3,5};
int serial=3;
double next_prime()
{
double f=primes[serial-1]+2;
int count;
for(count=1;primes[count]<=(sqrt(f)+1) && count<serial;count++){
if(fmod(f,primes[count])==0){
f+=2;
count=1;
}
}
return primes[serial++]=f;
}
int main()
{
double ugly_serial=12,ugly_number=16,j;
int c,count,loop,input;
scanf("%d",&input);
while(ugly_serial<input)
{
loop=0;
for(c=3;primes[c-1]<=sqrt(ugly_number);c++){
j=next_prime();
}
for(count=3;count<c;count++){
if(fmod(ugly_number,primes[count])==0){
loop=1;
break;
}
}
if(loop==0){ugly_serial++;}
ugly_number++;
}
printf("%.0lf",ugly_number);
return 0;
}
I have compiled and run your code. The program works fine with all the input I have tried, including 56565.
Are you sure you are running the most recently compiled version of your program?
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i am getting runtime error sigsegv, i don't understand the problem with this code.
this is a program to match initial substring with with array of string having maximum priority.
#include<stdio.h>
#include<limits.h>
int main() {
int T,i,N;
char si[T][1000];
long vi[T];
scanf("%d",&T);
for(i=0;i<T;++i)
scanf("%s%ld",&si[i],&vi[i]);
scanf("%d",&N);
while(N--) {
char str[1000];
scanf("%s",str);
int j,maxPindex=-1;
long maxPriority=LONG_MIN;
int l=strlen(str);
for(j=0;j<T;++j) {
if(strlen(si[j])>=l && strncmp(str,si[j],l)==0 && vi[j]>maxPriority) {
maxPriority=vi[j];
maxPindex=j;
}
}
//free(str);
if(maxPindex==-1) printf("NO\n");
else printf("%s\n",si[maxPindex]);
}
return (0);
}
It crashes already when it tries to create the variable "si". A C variable inside a function, such as the variable "T", starts with random garbage as its value. For example, it could contain 918128238. Then, when trying to create "si", this would be a very large array, and it doesn't fit.
You need to read a value for "T" before "si" and "vi" are created. That is, move your scanf before the declarions of "si" and "vi".