This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 6 years ago.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int dorand(){
int i;
srand(time(0));
i = rand()%3+1;
return i;
}
int main (){
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
return 0;
}
The issue is: the four printf are printing the same number.
When I do the rand() directly in the main function there is no problem at all but when I call a function to do so the random generation gets addicted to the same number. Do someone have some experience to share, please?
I've tried:
int main (){
srand(time(0)) //seeding in the main function before calling the dorand function
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
printf("\n %d \n", dorand());
return 0;
}
Also
int dorand(){
int i;
i = 0; //clearing the variable before attributing a new rand value
srand(time(0));
i = rand()%3+1;
return i;
}
Sorry if I mistook something, thanks for helping
The srand function seeds the random number generator. For a given seed value, the same set of random numbers gets generated.
Since you re-seed each time you want a random number, using the current time as the seed, assuming each call to the function happens in the same second the random number function is seeded with the same value, so you keep getting the same "random" numbers.
You should call srand only once at the beginning of your program. Remove the call from dorand and put it at the top of main.
Related
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}
My teacher isn't willing to help me with my error so I don't know where else to go. On line 19, addition();, the error says that there are too few arguments in the function call and I'm not sure why this is. I am a beginner programmer, but I have called functions before so I'm not sure why I am getting a problem now.
#include <stdio.h>
int addition(int *change);
int main(void)
{
int num = 10;
printf("Name \t Address \t Value\n");
printf("%s \t %p \t %d\n", "num", &num, num);
int *change = #
printf("Change: %p\n", change);
*change = 100;
printf("The value of num is %d \n", num);
printf("The value of change is %d \n", *change);
addition();
return 0;
}
int addition(int *change)
{
int input;
int result = input + *change;
printf("Input a value ");
scanf("%d", &input);
printf("The result will be change (%d) + input (%d)\n", *change, input);
printf("Result: %d", result);
return 0;
}
Perhaps this would be better suited to a comment, but I lack the required reputation to post comments...
When you're calling a function, you often have to supply some information. If I walked up to you and commanded "Add!" you might reply "what should I add?" This is essentially what your error message is telling you. You're issuing a command, but you're not giving it enough information to complete that command.
You can find what additional information is required by a function by glancing at its declaration. In this case, your function declaration is:
addition(int *change)
meaning that, in order to function properly, the function requires a pointer to an integer (int *). Every time you call the addition function, you have to supply this argument so that the function knows the number to which it is expected to add.
Program (in C) is to ask user to input integers one at a time (0 is quit indication) and find the number and total of the even inputs/odd inputs using while loop, if/else structure, and user defined function. I can't get the user defined function to print the statements needed.
(Code so far below)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char name[30];
printf("Enter name: ");
scanf("%s", name);
int num=1, even_count=0, even_sum=0, odd_count=0, odd_sum=0;
while (num != 0)
{
printf("Enter an integer (0 to stop): ");
scanf("%d", &num);
}
if ((num % 2) == 0) {
even_count++;
}
else {
odd_count++;
}
printf("%s,your inputs are broken down as follows: \n", name);
return even_count, even_sum, odd_count, odd_sum;
}
int output_function(int even_ct, int e_sum, int odd_ct, int o_sum)
{
int count1, sum1, count2, sum2 = main();
printf("You entered %d even numbers with a total value of %d.\n", count1, sum1);
printf("You entered %d odd numbers with a total value of %d.\n", count2, sum2);
return 0;
}
You never call output_function.
Replace
return even_count, even_sum, odd_count, odd_sum;
by
output_function(even_count, eve_sum, odd_count, odd_sum);
and remove
int count1, sum1, count2, sum2 = main();
The last line to remove makes absolutely no sense.
and finally replace your printfs by this:
printf("You entered %d even numbers with a total value of %d.\n", even_ct, e_sum);
printf("You entered %d odd numbers with a total value of %d.\n", odd_ct, o_sum);
I think, first you should declare the output_function before the main:
int output_function(int even_ct, int e_sum, int odd_ct, int o_sum);
int main()
{...}
Second, the return value of the main must be a single integer value.
Third, the output_function return always a zero.
And don't forget to call the output_function.
SK
You have some nice thoughts.
You thought:
output_function is defined after main so it can call main
A function can return several variables which in order:
return even_count, even_sum, odd_count, odd_sum;
And you can get the value of them by calling the function:
int count1, sum1, count2, sum2 = main();
But C does not work like that:
main function is executed the very first, earlier than any other functions.
You cannot return several variables like that in C (in some other high level languages like Python, it is ok).
return even_count, even_sum, odd_count, odd_sum; equals to return odd_sum;
Similar above point, int count1, sum1, count2, sum2 = somethings; just bring you sum2 = somethings
Please clean these mess first and you might get the program work well.
I'm studying buffer overflow, and I'm trying to jump to the function 'confused' and then print out "done" at the end of main by performing buffer overflow.
#include<stdio.h>
#include<stdlib.h>
int i, n;
void confused(int i) {
printf("**Who called me? Why am I here?? *** %x\n ", i);
;
}
void shell_call(char *c) {
printf(" ***Now calling \"%s\" shell command *** \n", c);
system(c);
}
void victim_func(){
int a[4];
printf("\nEnter n: "); scanf("%d",&n);
printf("~~~~~~~~~~~~~ values and address of n locations ~~~~~~~~~~");
for (i = 0;i <n ;i++)
printf ("\n a[%d] = %x, address = %x", i, a[i], &a[i]);
printf("\nEnter %d HEX Values \n", n);
// Buffer Overflow vulnerability HERE!
for (i=0;i<n;i++) scanf("%x",&a[i]);
printf("Done reading junk numbers\n")
}
int main() {
printf("\n ~~~~~~~~~~~~~~~~~ Info Menu ~~~~~~~~~~~~");
printf("\n addrss of main %x", main);
printf("\n addrss of shell_cal %x", shell_call);
printf("\n addrss of confused %x", confused);
victim_func();
printf("\n done");
return 0;
}
What I did is I put 7 for n, and for 6th hex value I inserted the address of confused and for 7th the address of printf in main. It successfully prints out "done" after the confused function, but the program goes back to the start of main. I thought the program would terminate after printing out "done".
I just wonder if I did something wrong, or it is the way it should do.
You can always call exit() in your shell code to terminate the program. However, you can't do it using system(), because system() will create a child process which always ultimately return to it parent. You need to directly call exit() using assembly.
This question already has answers here:
srand() — why call it only once?
(7 answers)
Closed 9 years ago.
So, I'm currently reading a book about C and, in an exercise, I should do a program that would get input from user (a number between 1 and 12) and would "roll" that number of dice, and then display the results.
The problem is, when it randomizes the dice numbers, all results are exactly the same. And I did use "srand((unsigned)time(NULL))" to seed. What could possibly be going wrong?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int throw(void);
int main()
{
int many,x,sum;
sum = 0;
puts("R O L L ' E M !");
type:
printf("How many dice would you like to roll (1 - 12)? ");
scanf("%d",&many);
if(many>12) {
puts("You can roll up to 12 dice!");
goto type;}
if(many<1) {
puts("You need to roll at least one die!");
goto type;}
int num[many];
printf("\nRolling %d...\n",many);
puts("Here they come!");
printf(" ");
for(x=0;x<many;x++) /* Shows which die it is */
{
if(x>=9)
printf("%d ",x+1);
else
printf(" %d ",x+1);
}
putchar('\n');
for(x=0;x<many;x++) /* Thingy to illustrate the dice */
printf("+---");
puts("+");
for(x=0;x<many;x++) /* Shows dice results */
{
num[x] = throw();
printf("| %d ",num[x]);
sum = sum + num[x];
}
puts("|");
for(x=0;x<many;x++) /* Thingy to illustrate the dice */
printf("+---");
puts("+");
printf("Total = %d",sum); /* Shows total */
return(0);
}
int throw(void) /* "Throws" the dice, by randomizing a number between 1 and 6 */
{
int n;
srand((unsigned)time(NULL)); /* seed */
n = rand() % 6 + 1; /* randomizes and limits from 1 to 6 */
return(n);
}
You are calling srand before for each call to rand. srand initializes the algorithm with a seed. Probably because of the short time between the calls, the timestamp is the same, therefore the seed. As a result, you reinitialize the algorithm with the same value, therefore producing the same sequence of numbers. The solution is to call srand only once per thread.
you have to use srand() just one time at the beginning of your program, otherwise the rand() function uses the same seed for all the calls since srand is called many times in a very small time lapse, shorter than a second and the seed remains the same for all the calls..