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 2 years ago.
Improve this question
Im writing a code in C for college that involves the game rock(0)-paper(1)-scissor(2)-ecshe(3). The game should run with random numbers for both Player. In the end I have to get statiscal date of how often one player has won and how often rock(0)-paper(1)-scissor(2)-ecshe(3) apperead.
When I write a single code I can get all the information.
But when I am using a function I get only zero to calculate the statiscal data.
The value for Player_1 is always set as rock(1) in one of the rounds. The function is to calculate how many times Player_2 got paper(2) or scissor(3)
Making long story short:
How do I create a function to return me to possible answer according to the condition?
int gewinn_summe_a(int spl_2, int a, int b , int c, int d){
if(spl_2== a){
return ++b; //the counter for a variable that i have in the main function
}else if(spl_2==c){
return ++d;
}
// return 0;
if (player1 == 0) //this if set the value for player 1
if (player2 == 1 || player2 == 3){ //this 'if' says that the player2 can have this two option
//When the code below inside the program it does what is suppose to do:
//calculate how many time player2 gave option 1 or how many time he gave option3
if(spl_b==1){
count_sh_b++;
}else if(spl_b==3){
count_ec_b++;
}
//However, when i use the function, I always get zero as the counter.
gewinn_summe_a(1, count_sh_b , count_ec_b, spl_b)
printf("Contador SH B %d\n", count_sh_b);
printf("Contador EC B %d\n", count_ec_b);
printf("Spieler A: %d - %s\n", taste ,spiel_name[taste]);
printf("Spieler B: %d - %s\n", spl_b ,spiel_name[spl_b]);
printf("Player 1 A WON\n\n");
You need to pass the counters by reference, like this:
int gewinn_summe_a(int spl_2, int a, int *b, int c, int *d)
{
// ...
}
Then you call it by giving the addresses of the caller's variables:
gewinn_summe_a(spl_b, 1, &count_sh_b, 3, &count_ec_b)
BTW, your source is terrible, because it is incomplete, bad indented, and the variable names are mindlessly chosen.
Related
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 1 year ago.
Improve this question
We were tasked to make a program about Coin tossing. I would like someone to recheck my code whether there is a need to be changed or clarified.
This is the question:
Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times, and count the number of times each side of coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time for a total of approximately 50 heads and 50 tails.]
Note: Function Calls should be applied to the program.
My Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int flip();
int main ()
{
int coin, counter, tails = 0, heads = 0;
printf("\nA program that simulates Coin Tossing with a probability of getting half of Heads or Tails\n\n");
for (counter = 1; counter <= 100; counter++)
{
coin = flip (); //Function Call
if(coin == 0)
{
printf("T ");
tails = tails + 1;
}
else if( coin == 1)
{
printf("H ");
heads = heads + 1;
}
}
//Total Count of Heads and Tails
printf("\n\n--- TOTAL COUNT ---\n", heads);
printf("Heads was tossed %d times\n", heads);
printf("Tails was tossed %d times\n", tails);
}
//Approximately 50 Heads and 50 Tails
int flip()
{
return rand( ) % 2;
getch();
}
One oversight (remove the heads):
printf("\n\n--- TOTAL COUNT ---\n", heads);
you may also want to remove getch(); inside flip() and fix the indentation of the overall file if that matters.
NO need to define function signature above the main and implement later. You can do this in one way.
Need to return 0 in your main function(last line in your main function).
Don't use unnecessary header unless you use their function/value.
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 2 years ago.
Improve this question
Why is the variable input2 assigned -1 and why does it give me a memory value when I run the code?
#include <stdio.h>
int main(void)
{
int input1;
int input2 = -1;
while(input2 != 0)
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
}
if(input1==99)
puts("Finish.");
return 0;
}
You have a while loop that continues as long as input2 is not equal to 0, so in order to enter it the variable needs to be initialized with some arbitrary value that isn't 0. Functionally, you could have used 1, -123, 999 or any other non zero value and get the same effect.
Arbitrary value with no reason. If uninitialized, it will cause undetermined
behavior whether the while loop is taken or not (depending on what garbage is inside input2). The writer of this code wanted the while loop to enter. Could have rewritten it
int input1;
int input2;
do
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
} while(input2 != 0);
#Jonas Rye Nielsen this program is just a simple calculator to add 2 Integer number and that's it.
Now in order to quit and program, what it does is to check the second Input with some logic behind it.
If you enter a 0 to add 2 integer (or anyway in any kind of addition really) add a 0 would just make no sense at all, hence the progam is implicitly assuming that you want to exit it!.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm writing a program that calculates results of football matches.I'm trying to store the team ids' in an array with a length of 10 defined at the top but i keep getting getting a build error from the array. I realise the syntax might be wrong but how else can i use a variable to specify array length?
The error message i'm getting is : expected expression before '{ ' token.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
int main() {
int numberofmatches, hometeamid, awayteamid, hometeamgoals, awayteamgoals;
int hometeamwins = 0;
int winratio;
int teamid[ARRAY_SIZE];
printf("Enter number of matches played \n");
scanf("%d", &numberofmatches);
if (numberofmatches > 0) {
int x = 0;
do {
printf("Enter match stats in order Home_team_ID,Away_Team_ID,Goals_Home,Goals_Away\n");
scanf("%d %d %d %d", &hometeamid, &awayteamid, &hometeamgoals, &awayteamgoals);
teamid[ARRAY_SIZE] = {hometeamid}; //Error is on this line
if (hometeamgoals > awayteamgoals) {
hometeamwins++;
}
x++;
}
while (x < numberofmatches);
winratio = hometeamwins / numberofmatches;
printf(" %d :teamidth %d :winratio", teamid[0], winratio);
}
return 0;
}
This
teamid[ARRAY_SIZE] = {hometeamid};
is the syntax for defining an array of size ARRAYSIZE and initialising it incompletely.
You try it in the middle of a loop.
In case you want to write to an array member you probably want
teamid[x] = hometeamid;
Also, I recommend making sure that you do not write beyond teamid[9], which is the last legal member of the array, for ARRAY_SIZE == 10.
Your defining an array of size 10, and then accessing it at index 10. Since arrays are indexed 0..n-1 you need to access index 9 to get the end of the array instead of 10.
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 years ago.
Improve this question
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);
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 7 years ago.
Improve this question
I have a c code
int foo( int *p, int n){
int a[81];
int i,v;
for( i = 0; i < n ; i++){
a[i]=p[i]
if( n == 1 ) return a[0];
v=foo ( a, n-1);
if( v > a[n-1] )
return v;
else
return a[n-1];
}
int main ( void ){
int b[81], i;
for( i = 0; i < 81; i++){
b[i]=rand();
foo(b,81);
return 0;
}
And i need to find out how many instances of variable a will exist( max number) my answer was 81 , but it was wrong and i cant find out what number it should be. How could i determine it?
The main will call the function 82 times and each time the func will call recursively itself 80 times in a loop of decreasing n items.
So altogether it will be 81*81.
EDIT: I didn't notice the return after first iteration so actually it's pretty small number.
Set the number of elements in a to be arbitrarily high.
Build a function to assign elements to a, and only use that function to set elements of a. You could use this function to make sure that you don't attempt to write outside the bounds of a. (Else the behaviour of your program will be undefined and so any results will be meaningless).
Encode a "watermark" static int in that function to record the largest index accessed.
Run your program and note the value of the variable set up for (3).
Declare a global counter - it will be initialized to zero. In the function, at the same scope as 'a' is declared, increment the counter.
Printf() it out at the end of main().