find sum of infinite series.Why does it issue a zero amount? - c

You need to find the sums of an infinite series with a given accuracy.(the picture with the task is given as a link)
the program constantly counts the zero amount. I don't understand what my mistake is
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int i;
int n =1;
double E,x,q;
double sum = 0;
scanf ("%f",&E) ;
scanf ("%f",&x) ;
q=pow(x,3)/6;
while(fabs(q)>=E){
if (n/2==0) {
sum=sum+q;
}
else {
sum=sum-q;
}
q=(q*pow(x,2))/(n+3);
n=n+1;
}
printf("%f",sum);
return 0;
}

It will never going to enter to that if statement. You are letting n=1 and it will be always 1, and u are doing n=n+1 in the else.

Related

Why do I get the wrong answer from this C code?

#include <stdio.h>
#include <stdlib.h>
#include "simpio.h"
#include "stdio.h"
int main()
{
float answer;
int D;
int N;
int i=0;
int p=1;
printf("How much :");
N=GetInteger();
for (i=0; i!=N; i++)
{
for(p=1; p!=N; p++){
D=1/p;
answer+=D;
}
}
printf("the answer is: %.2f",apotelesma);
return 0;
}
for example if I gave N=100 then the program was suppose to
1/1+1/2+1/3....1/N
and then give me the 5.19
but for some reason its just skips it
I know it's an easy question I started programing like for two weeks ma trying to learn alone.
Replace the inner-loop with this to force floating-point math:
for(p=1; p!=N; p++)
answer += 1.0/p;
Also initialise float answer = 0;.

Perfect numbers representation problem with function

The task is:
Write a program that prints the first n perfect numbers. Checking that the number is perfect should be done in the perfect function.
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
int perfect_number(long int n)
{
long int sum=0;
for(int j=1;j<n;j++)
{
long int candidate=j;
if(n%candidate==0)
{
sum=sum+candidate;
}
}
if(sum==n)
{
return n;
}
return 0;
}
int main()
{
int n;
printf("Enter how much perfect numbers you want :");
scanf("%d",&n);
while(n<1)
{
printf("Enter how much perfect numbers you want:");
scanf("%d",&n);
}
int counter=0;
for(long int i=1;counter<n;i++)
{
if(perfect_number(i))
{
printf("%ld ",i);
counter++;
}
}
return 0;
}
The problem arises when I type that I want, the first 5 perfect numbers or more. The program will print only 4 and will continue to work, it will search for numbers but will not print anything.
If I type in the first four perfect numbers, they will print 4 and finish the program, they will do everything right.
I thought the problem was in representing the fifth number, so I replaced int with long int, but that didn't help.

How to quit loop when requirement is met in C

I have made a while loop and it works partly. I want the code to stop when the values entered are under the parameter, but it keeps going regardless of the output. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
// defining variables until "till here" comment
int i;
int rollDice;
int firInp;
int secInp;
srand (time(NULL)); // seeding rand so that we get different values every time
// till here
while(rollDice > 0)
{
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): "); // prints the message
scanf("%d", &firInp); // user input stored into firInp
printf("Enter the amount of throws you want(MAX=499, MIN=1): "); // this message is printed after the users first input
scanf("%d", &secInp); // user input stored into secInp
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters
for(i = 0; i < secInp; i++){
rollDice = (rand()%firInp) + 1;
printf("%d \n", rollDice);
}
}
else{
printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
}
}
return 0;
}
I'm new to C btw.
edit: I want the loop to continue if the user input is more than 24, 499 respectively.
What you're doing is wrong. Variable rollDice is for storing the values of the outcomes rather than doing a condition check. It will have random values and since the values on the dice can't be negative or zero it may not exit the while loop. I don't know what will rand() will produce so I'm just assuming.
The range for rand() is [0,RAND_MAX), including zero and excluding RAND_MAX. But because of this expression (rand()%firInp) + 1 , you're adding one to it. So it will never become Zero.
You can use a flag variable and set it to 1. When the if conditions are met, you can set the flag to 0. It will exit the while loop.
Corrected code :-
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
int main()
{
// defining variables until "till here" comment
int i;
int rollDice;
int firInp;
int secInp;
int flag = 1;
srand (time(NULL)); // seeding rand so that we get different values every time
// till here
while(flag)
{
printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=1): "); // prints the message
scanf("%d", &firInp); // user input stored into firInp
printf("Enter the amount of throws you want(MAX=499, MIN=1): "); // this message is printed after the users first input
scanf("%d", &secInp); // user input stored into secInp
if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters
for(i = 0; i < secInp; i++){
rollDice = ((rand() + 1)%firInp);
printf("%d \n", rollDice);
}
flag = 0;
}
else{
printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
}
}
return 0;
}
EDIT :-
Also, division with 0 is undefined. rand() can attain value 0. You should add 1 to rand() rather than adding to whole modulus. It can create an error if the rand() will give 0 as an output.

Simple C Program, Unknown output

Problem Statement
You are given an array of integers of size . You need to print the sum of the elements of the array.
Note: A signed 32-bit integer value uses bit to represent the sign of the number and remaining 31 bits to represent the magnitude. The range of the 32-bit integer is . When we add several integer values, the resulting sum might exceed this range. You might need to uselong long int in C/C++ or long data type in Java to store such sums.
Input Format
The first line of the input consists of an integer. The next lines contain space separated integers describing the array.
Constraints
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005`
Sample Output
5000000015
My program
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[5],sum;
for(i=0;i<=n-1;i++){
scanf("%ld %ld %ld %ld %ld",&a[1],&a[2],&a[3],&a[4],&a[5]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
Error description
Input (stdin):
5
1000000001 1000000002 1000000003 1000000004 1000000005
Your Output (stdout):
140692151765426
Expected Output:
5000000015
Compiler Message:
Wrong Answer
This works fine :
#include <inttypes.h>
int main()
{
int n,i;
scanf("%d",&n);
unsigned long long int a[5];
unsigned long long int sum=0;
for(i=0;i<n;i++)
{
scanf("%llu",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%llu\n",sum);
sum = sum + a[i];
}
printf("\nSum is : %llu",sum);
return 0;
}
Use the ll long-long modifier with the u (unsigned) conversion
You don't basically need an array here. Just
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
long long int a, sum = 0;
while (n--) {
scanf("%lld", &a);
sum += a;
}
printf("%lld", sum);
return 0;
}
The program has following issues:
Array indexing is wrong : Array is of size 5 starting from 0 , you can use only till a[4] , but for scanf() , it tries to read value as &a[5].
No use of for loop when you are using hard coded index a[1],a[2],etc. Instead the below code will be better to get input as follows:
for (i = 0; i < n; i++ ) {
scanf("%ld",&a[i]);
}
Sum includes : sum = sum + a[i];
when i = 0 --> a[0] will have have junk values because input was not taken from user as scanf started from a[1] . Since array is not initialized this is uninitialized auto variable, it might have junk values which will get added in the sum .
sum itself is also not initialized, so it will contain junk value and for very first addition : sum = sum + a[0]; thsi junk value will get added .
Hope this answer your query for unexpected output.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,i;
cin >> n;
long long int s=0;
long int a[10];
if(n>=1 && n<=10)
{
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
}
cout<<s;
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[10];
long long sum=0;
for(i=0;i<=n-1;i++){
scanf("%ld",&a[i]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}

Guess the number game missing a loop

I was trying to do a "Guess the number" game that would use a verification to see if the number is low, high or equal to the random number. I want to do a loop verification that will say if the number is to high and to try again!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <cs50.h>
int
main(void)
{
srand(time(NULL));
int skittles = rand() % 1024;
printf("%d\n", skittles);
printf("choose a number betwen 1 to 1024\n");
int numpessoa = GetInt();
printf("%d\n", numpessoa);
if (numpessoa < skittles)
{
printf("numpessoa < skittles");
numpessoa = GetInt();
}
if (numpessoa > skittles)
{
printf("numpessoa > skittles");
numpessoa = GetInt();
}
if (numpessoa == skittles)
{
printf("numpessoa == skittles");
}
return 0;
}
If this is an homework,you shouldn't ask its solution directly.
I only can provide some pseudocode.
loop forever
get user's guess
check guess against answer
guess differs answer ,then provide some hints
guess equals answer,tell user he/she is right and break out of this loop
And there is an another bug in your code.
The 1024 modulo of an random integer is between 0 and 1023.

Resources