random number C [closed] - c

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.
Have to write a function return a square of number from 50 to 100, without using a seed and take no parameters.
I know how to generate a random number in a fixed range (for example 0 to 99):
int n;
n=rand(100); /* n is random number in range of 0 - 99 */
EDIT:
By the way, I used rand() and every time I run it give me same result. I know it because of my compiler, is there anyways to change it (remember not using a seed)
Any idea is much appreciated!
EDIT 2: OK I figured out how to fulfill all requirement. Thank you all!
Massimiliano is the first one who give me correct IDEA how to do it.

This is definitely more of a math question than a programming question, but the answer is in the question:
int n = random(51)+50;
return n*n;

Chose a random number in the range of 50 to 100. Then return its square.
int func(){
int r = random(51); //to choose a random number from 0 to 50.
r = r + 50; //to scale it to fit form 50 to 100
return r*r;
}

Related

C code randomize [closed]

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 randomize a variable and then trying to use it in the functions where I want to use. But, when I put the randomized variable in for loop like below and when I use that in the function that I want to then it gives me error.
#include <stdlib.h>
#include "time.h"
void main (void) {
for (int i = 0; i < 4; i++) {
srand( time(NULL ));
float r;
r = rand()*1000;
}
write(abc, r);
read(abc, r);
write(xyz, r);
read(xyz, r);
}
So, when I use r from the for loop I get the error below:
In function 'void sim()':
'r' was not declared in this scope
But, when I remove r from the for loop then there is not any error. But, I want for loop so as to have different data for every write function.
Any suggestions would be appreciated.
r is a local variable for the loop and it is not visible outside of the loop. To fix this place its declaration(float r;) before the loop.
The scope of r is only inside the for loop. Declare it prior to the loop, and you'll be good to go.

how can I covert int to char in C [closed]

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'm trying to covert an int to char. Is there any way to do that?
For example:
{
int i;
char d;
i = 55;
d = i;
printf("%c\n", d);
}
How do I make d = 55?
If you want to put the number 55 into a string, use sprintf
Indeed your example can do what you want.
If you really want to place safe, you may:
d = (char) i;
Try this code segment:
printf("%d\n", d);
char are presented in the memory as binary format wich is equivalent to a number and this number is called a code ascii. when you print the code ascii with "%c" Then it will print the charchter equivalent to this code ascii

Frequency of values of an array [closed]

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

C printing float arrays into columns [closed]

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'm trying to print out an array of 100 lf values into columns of ten. I was planning on inserting a new line whenever I hit the tenth data value in each line, but I'm having trouble with my if statement. "Left operand must be 1-value" is the error I keep running into.
Here's the code as is:
for (x=0; x<100; x++)
{
if (x % 10 = 0)
{
printf("\n");
}
printf("|%-6.2lf|", i[x]);
}
Is there a cleaner way to do this?
The problem is this line:
if (x % 10 = 0)
That should be the double-equals sign:
if (x % 10 == 0)
A common typo.

don't know why the program is crashing [closed]

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?

Resources