Generating the same sequence of random number in a loop [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A for loop to be exact. I've tried using srand() to seed the loop but it doesn't seem to be working. Could someone clarify the seeding part? Anyways, could someone point out my errors?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int a,b,c,d,loop1=0,loop2=1,count=0;
srand(1);
while(loop1!=1)
{
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3)
{
break;
}
}
return 0;
}
The output should look like this:
1 2 3 4
1 2 3 4
1 2 3 4

You need to set the seed at the beginning of the loop if you want to have the same sequence in each iteration:
while(loop1!=1) {
srand(1);
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
printf("%d %d %d %d\n",a,b,c,d);
count+=1;
if (count>3) {
break;
}
}

Related

Why am I getting an error in this C program? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I am new to C programming but I stumbled on this code
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
I ran the code and it gave me an output of 00246
why is that the output that, looking at it logically, the answer is not suppose to start with a 0
print(4) -> print(3) -> print(2) -> print(1) -> print(0) -> print(-1)
print(-1) stops the recursion returning 0, thus a call to printf() is emitted with 0 + 0, which is 0.
print(0) ends with -1 as value, and a call to printf() with 1 + -1 is emitted, which is 0.
etc.

warning: no description found for function main using betty compliant syntax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm learning the programming language C and using the Betty coding style of writing C github.com/holbertonschool/betty),
I have have been getting this syntax warning.
#include <stdio.h>
int main(void)
{
int a;
printf("\n Enter: ");
scanf("%d", &a);
return (0);
}
total: 0 errors, 1 warnings, 8 lines checked
c:2: warning: no description found for function main
After including the library, you ought to include a description of the program, like so:
#include <stdio.h>
/**
* main - Entry point
*
* Description: 'the program's description'
*
* Return: Always 0 (Success)
*/
int main(void)
{
Code goes here
}

How can I view a number of user groups? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I view a number of user groups?
I mean the implementation in C.
I wanted to use the getgrouplist() function, but I want it to take the number of groups automatically.
Here's an off the cuff program which seems to work on my macOS 10.14.1 system, which seems to be quite behind the times:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int res = 0;
int ng = 100;
int gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
Here's one which works on Ubuntu 16.04:
#include <stdio.h>
#include <grp.h>
int main(void)
{
int res = 0;
int ng = 100;
gid_t gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
The size 100 was chosen arbitrarily to provide maybe enough space.

Can anyone explain and trace the following multiple recursive C program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to understand how recursion works. I seem to not understand how this multiple recursion works. Please help and thanks!
Here is the output
#include<stdio.h>
int R(int x);
int main()
{
R(5);
return 0;
}
int R(int x)
{
if(x > 0){
x--;
R(x);
R(x - 2);
printf("%d ", x);
}
}
R(5) calls R(4) which will display something and then R(2) which will also display something. After that R(5) displays 4. Which is why you see a 4 at the end of the output.
R(4)display - R(2)display - 4
Before the 4 you have the display of R(2). R(2) calls R(1) which displays somenthing and then R(0) which displays nothing. R(2) displays 1. Which is why you have a 1 at the end, just before 4.
R(4)display - R(1)display - R(0)display - 1 4
And so on...
R(4)display - R(1)display - 1 4
R(4)display - 0 1 4
You always process the R()display calls starting from the end (right side first).
As you can see recursivity will inverse the order of the numbers (4, the biggest number, is last). This is because you have the printf after the recursive calls to R.
Take a look at the trace tree I made
R(5) means the call of R function for 5, and as you know the output of it will be 4. The numbers show in which order the functions are executed till the end. The ones that have a tick besides them will make outputs.

About tail recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Recently I am reading Mastering Algorithms with c, and in this book I have 1 exercise that I am not able to implement with c.
Tn = 1 if n=1 ,
Tn = 2T(n/2) + n if n > 1
Anyone can help me? I'll appreciate it a lot.
I have tried.
#include <stdio.h>
int test(int n) {
if (n == 1)
return 1;
else if( n > 1 )
return test(n / 2) * 2 + n;
}
int testtail(int n, int running_result) {
if (n == 1)
return running_result;
else
**return testtail(n / 2, ???? );** // How can I implement the second param
}
I am sorry guys! I am not a native English speaker! Maybe I made some mistakes in grammer! I should apologize for this!

Resources