Coin Tossing - Simple Program [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 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.

Related

How to maximize the number of teams of 3 players [closed]

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 2 years ago.
Improve this question
There are N students in a class, and Teacher wants to divide these students into some groups. Teacher says that groups consisting of two or less students is not allowed, so Teacher wants to us to have as many groups consisting of three or more students as possible.
Divide the students so that the number of groups consisting of three or more students is maximized.
I have written the code up to the following, but it is not giving the correct result for some test cases. Could anyone please tell me what's wrong
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
if(n%3 == 0){
printf("%d", n/3);
}
else if(n%4 == 0 && n/4 == n/3){
printf("%d", n/4);
}
else if((n-4)%3 == 0){
printf("%d", ((n - 4)/3)+1);
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
printf("%d", n/3);
}
Let me know it outs right answer
I believe this is the most simple one
💎
You're not printing anything for (e.g.) n = 20.

C function with if return [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 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.

Continue statement in If Else condition gives infinite loop [closed]

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 3 years ago.
Improve this question
I was solving a problem on strings
Given a string S, write a program to title case every first letter of words in string.
Input:
The first line consists of an integer T i.e number of test cases. T testcases follow. The first and only line of each test case consists of a string S.
Output:
For each testcase, in a new line, print the required output.
Constraints:
1 <= T <= 100
1 <= |S| <= 1000
Example:
Input:
1
I love programming
Output:
I Love Programming
and for that I came up with this solution.
#include <stdio.h>
#include <string.h>
int main() {
//code
int t,flag;
scanf("%d", &t);
while(t--){
int n,i=0;
scanf("%d", &n);
char str[100];
scanf("%s", str);
while(i<n){
if (str[i]!= str[n-1-i]){
flag = 1;
printf("%d", flag);
break;
}
else{
flag = 0;
printf("%d", flag);
continue;
}
i++;
}
if(flag ==1 )
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
This code works fine when continue is removed, but when the above code is run, it prints 0 infinitly.
Can you help me where I'm going wrong?
Thanks in advance.
The "continue" here means "immediately perform the next iteration in the current loop, in this case the while loop".
In your code the line "i++" is therefor not executed and variable i never changes, thus causing an infinite loop.

Thinking of a code to show if a number is prime or non prime? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.

What does this mean regarding loops? [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 years ago.
Improve this question
i passed this project in and the grader gave me 50% saying i did not use function for loops. The instructions said use while loops, or does he mean otherwise? here is the code. the project is supposed to count from 10 to 0 then 0 to 10.
#include <stdio.h>
int main()
{
int Integer;
printf("Please enter an integer\n");
scanf_s("%d", &Integer);
int count = Integer;
while (count >= 1)
{
printf("%d\n", count);
count--;
}
printf("*****\n");
while (count <= Integer)
{
printf("%d \n", count);
count++;
}
return 0;
}
This may be related to a specific style the grader wanted your class to learn, or a specific conversation. I suggest asking, as your grader's response was (clearly) missing some details for you.
Meanwhile, some suggestions of what your grader might have been looking for.
Did your grader literally mean for you to use for-loops?
for ( ; count >= 1; count--) {
printf("%d\n", count);
}
Did you forget to count to 0 the first time? (The above loop will stop printing at 1, not 0.
Does your grader want you to functionalize the loop kernels?
void countDownLoopKernel ( int value ) {
printf("%d\n", value);
}
...
while ( count >= 1 ) {
countDownLoopKernel( count );
count--;
}
For a functioning program, items 1 and 3 are arbitrary. They can be crucial when fitting into a larger program's (or company's) style, for readability, for following DRY principals, or for refactoring, but for small programs like this, they make no difference. I suspect your grader is trying to get you to think about alternatives beyond "It works, so it's good enough."
They may have wanted you to use both for and while loops. To count from Integer to 1, try this:
for (count = Integer; count >= 1; count--)
printf("%d\n",count);
Also, to count from 1 to Integer, try this:
for (count = 1; count <= Integer; count++)
printf("%d \n");
I hope this helps!

Resources