for() and random() FUNCTIONS - c

Why is this code not working properly?
The intent of this code is to generate and print two random numbers separately
as many times as user choose.
M.T.
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) rand() % x
#define randomize srand((unsigned)time(NULL))
int i=0;
int j=0;
int x=0;
int y=0;
int main(void)
{
printf("insert number of loops:");
scanf("%d",x);
for(y=0;y=x;y++)
{
randomize;
i = random(51);
j = random(51);
printf("%d\n",i);
printf("%d\n",j);
}
return 0;
}

You are calling srand more than once. Place randomize; outside the for loop. Another problem is with the statement
scanf("%d",x);
you forget to place & before x.
Also You need to correct the loop condition y = x to y < x as mentioned in comment by #Cool Guy.

solved, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) rand() % x
#define randomize srand((unsigned)time(NULL))
int i=0;
int j=0;
int x=0;
int y=0;
int main(void)
{
printf("insert number of loops:");
scanf("%d",&x);
randomize;
for(y=0;y<x;y++)
{
i = random(51);
j = random(51);
printf("%d\n",i);
printf("%d\n",j);
}
return 0;
}

Related

which way is the correct way to write these two for loops?

I wish to create two loops with different integer outputs. so I can recognize which one is which. I will have to add many more loops like this into the main function so I'm looking for the easiest solution.
Is this one correct?
#include <stdlib.h>
#include <math.h>
#define MAX_CARS 1000
main()
{
double carsTimeheadA[MAX_CARS], carsTimeheadB[MAX_CARS];
int i,z,n;
int j,q,a;
n=100;
a=1000;
for(i=0; i<n; i++)
{
for (j=100; j<a; j++)
carsTimeheadA[i]=0.0;
carsTimeheadB[j]=0.0;
}
}
Or this one?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_CARS 1000
main()
{
double carsTimeheadA[MAX_CARS], carsTimeheadB[MAX_CARS];
int i,z,n;
int j,q,a;
n=100;
a=1000;
for(i=0; i<n; i++)
{
carsTimeheadA[i]=0.0;
}
for (j=100; j<a; j++)
{
carsTimeheadB[j]=0.0;
}
}
As far as I understand, you want the first 100 element of carsTimeheadA to have the values 1, 2, 3, 4, ..., 100 and the first 100 element of carsTimeheadB to have the values 101, 102, 103, 104, ..., 200
In that case you only need a single loop like:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_CARS 1000
int main()
{
double carsTimeheadA[MAX_CARS], carsTimeheadB[MAX_CARS];
int i, n;
n=100;
for(i=0; i<n; i++)
{
carsTimeheadA[i]= 1.0 + i;
carsTimeheadB[i]= 101.0 + i;
}
// ... more code
return 0;
}
You have a conflict between what you code and what your intention is, looking at the indentation. Your loop is, properly indented, as follows:
for(i=0; i<n; i++)
{
for (j=100; j<a; j++)
carsTimeheadA[i]=0.0;
carsTimeheadB[j]=0.0;
}
which is a bit nonsense and wrong: You loop 100 times or so over j to assign to an element i. After the loop, j probably points outside the array when you initialize the j element.
Do you mean the following?
for(i=0; i<MAX_CARS; i++)
{
carsTimeheadA[i]=0.0;
carsTimeheadB[i]=0.0;
}

C for condition tricks

I found a challenge on the internet and I'm really stuck.
The goal is to print 20 times _ by adding/changing only 1 character (only one operation performed in total):
#include <stdio.h>
int main(void)
{
int i;
int n=20;
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
I have already found 1 solution but I can't find the last one? Is there some tricks I need to know about for loops ?
Replace i by n
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i < n; n--)
printf("*");
getchar();
return 0;
}
Put - before i
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
printf("*");
getchar();
return 0;
}
Replace < by +
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i + n; i--)
printf("*");
getchar();
return 0;
}
Source: https://www.geeksforgeeks.org/changeadd-only-one-character-and-print-exactly-20-times/
to correct the posted code to only output 20 times, you could use:
#include <stdio.h>
int main(void)
{
int i;
int n=-20; // note the minus 20
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
If it is allowed you could write:
n=10; for(i=0;i<n;i++){printf("__");}
or
n=10; for(i=0;i<n;i++){printf("_");printf("_");}

Find how many positive numbers there are in my array

How to find how many positive numbers there are in my code? I am getting the wrong output, please explain in detail where my mistake here is. I wish to have the exact output as desired. If I entered 6 as n then I will insert six numbers and the output will show me how many positive numbers I have inserted.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n;
scanf("%d",&n);
int arr[n];
int i;
int p = 0;
int arr_i;
for (arr_i = 0; arr_i < n; arr_i++) {
scanf("%d",&arr[arr_i]);
}
for (i = 0; i < n; i++) {
int arr_index=i;
if (arr[arr_index] > 0) {
p++;
}
printf("%d",p);
}
return 0;
}
Try this:
Just Print p outside the for loop.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n;
scanf("%d",&n);
int arr[n];
int i;
int p = 0;
int arr_i;
for (arr_i = 0; arr_i < n; arr_i++)
{
scanf("%d",&arr[arr_i]);
}
for (i = 0; i < n; i++)
{
int arr_index=i;
if (arr[arr_index] > 0)
{
p++;
}
}
printf("%d",p);
return 0;
}
YOu need t allocate dynamic memory using malloc - hope this helps
#include <stdio.h>
#include <stdlib.h>
int main() {
char c;
int n;
scanf("%d", &n);
int * arr;
int i;
int p = 0;
arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
if (arr[i] > 0)
{
p++;
}
}
printf(" %d", p);
return 0;
}

C Program bigneer

I am working on generate 100 random numbers and place them in a , numbers should be 0-999. I wrote my program and it didn't print the random numbers.
I appreciate any help.
this is my code
#include <stdio.h>
#defin S 100
int main()
{
int x;
int a [S];
a[S]=100;
for(x=0;x<s;x++){
printf(a[x]);
}
return 0;
}
Two things: first, int a [S]; a[S]=100 exceeds the array bounds (max is S-1).
Second, printf(const char* format, ...) expects a format string, but you pass an integer value at the place of the format string (turn on compiler warnings!). So write printf("%d ", a[x]), and the program should at least print out some numbers (once you actually assign any numbers to a).
like this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define SIZE 100
#define RANGE 1000
int main(void){
srand(time(NULL));
int a[SIZE];
bool chosen[RANGE] = {0};
for(int i = 0; i < SIZE; ++i){
int select = rand() % RANGE;//select 0..RANGE-1
while(chosen[select]){//check duplicate
if(++select == RANGE)
select = 0;//reset
}
chosen[select] = true;//selected
a[i] = select;
}
//result
for(int i = 0; i < SIZE; ++i)
printf("%d ", a[i]);
puts("");
return 0;
}

first 50 positive numbers divisible by 4

This is the code I reached.
what I don't get is the condition where x should stop. I know what I wrote is wrong.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, i;
for(i=0; i<=50; i++)
{
for (x=0;x<=50 ;x++ )
{
if (x%4==0)
printf ("%d\n", x);
}
}
return 0;
}
The simplest solution, as suggested by Art in a comment above:
for(unsigned int c=1; c<=50; c++)
{
printf ("%d\n", c*4);
}
Original answer: One loop is enough. And increase by 4 (every 4th number is divisible by 4, and none else):
int curNum=4;
for (unsigned int c=1; c<=50; c++, curNum+=4)
{
printf ("%d\n", curNum);
}
So you see we have two variables, one counts the numbers we have printed already (c), and the other contains the current number to be printed (curNum).
With this code you are showing numbers less than 50 that are divisible by 4 & then you are printing it 50 times. Although your question has been answered but if you are hell bent on checking if a number is divisible by 4 & using two for loops, this might work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0, i;
for(i=0; i<=50; i++)
{
for(;;)
{
if(x%4==0)
{
printf("\n%d",x);
x++;
break;
}
x++;
}
}
return 0;
}
int main()
{
int x = 0;
int i = 0;
while(i<50)
{
if (x % 4==0)
{
printf ("(result %d) %d is divisible by 4\n", i, x);
i++;
}
x++;
}
return 0;
}
No need to use nested for loops. Try this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0, count = 1;
while(1)
{
if (i%4==0)
printf ("%d\n", i);
i++;
if (count++ == 50)
break;
}
return 0;
}
you just need one for
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned int n = 0;
unsigned int i = 0;
while(n < 50)
{
if (i%4==0){
printf ("%d\n", i);
++n;
}
++i;
}
return 0;
}

Resources