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

#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;.

Related

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

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.

i was not getting the result that I'm expecting from below code

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int sizze(int n) //gives size of binary number 0f decimal number n.
{
int count=0;
while(n>1)
{
n=n/2;
count++;
}
count =count +1;
return count;
};
int bin(int n) //gives binary number.
{
int l=0;
int y[sizze(n)];
int t=sizze(n);
for(int i=0; i<t; i++)
{
y[i]=n%2;
n=n/2;
}
for(int i=t-1; i>=0; i--)
{
l=l+y[i]*pow(10,i);
}
return l;
}
int main()
{
printf("%d",bin(5));
return 0;
}
the above code was to print binary number of a given number ,but there was some error.
I was expecting it to print 101 but it's printing 100.
there was some error in l=l+y[i]*pow(10,i); but I can't understand what was wrong with it.
can anyone help me with finding the mistake.
Generally, using floating point numbers while calculation can be done using only integers is not a good idea because floating point number calculations may contain errors.
(for more information, see language agnostic - Is floating point math broken? - Stack Overflow)
In this case, you can change calculation order of additions without changing their results, so the part
for(int i=t-1; i>=0; i--)
{
l=l+y[i]*pow(10,i);
}
can be written as
for(int i=0,delta=1; i<t; i++)
{
l=l+y[i]*delta;
delta=delta*10;
}

Function isn't working like it should. The execution stops

I am a beginner in c and i was writing this piece of code. This is the first time im using doubles so it might be related.
The code gives the print statement in the main function, then when it enters my function ReadVector() it stops working.
I want to to learn and fix my mistake, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%f",*(x + i));
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
Try this one:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%lf", &x[i]);
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",&m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}

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;
}

My code for Project Euler #8 is not working, could somebody help me review it

I did a course on C programming 2 years ago and I was trying to get back into it with the project Euler problems. My code is quite sloppy, sorry for that.
Problem 8 has you calculate the biggest product of 13 consecutive digits in the a very large 1000-digit number.
I wrote some code that uses fgets to read the pasted input and then convert the char ascii values for calculation and output.
My code returns the string of numbers 5576689664895, but this is apparently wrong. I have already tried to prevent overflow errors by using long long ints for all the numbers which could possibly get large.
#include <stdio.h>
#include <string.h>
int highest=0;
long long int highestproduct=0;
char string[1050]={};
long long int product(int i);
int main(){
fgets(string,1050,stdin);
for(int i=0; i<1050;i++){
if((int)string[i+12]==10){
i=1050;
}
else if(product(i)>highestproduct){
highestproduct=product(i);
highest=i;
};
}
for(int z=0; z<13; z++){
printf("%d", string[highest+z]-48);
}
printf("\n");
fflush(stdout);
}
long long int product (int i){
long long int interm=1;
int numbers=0;
for(int n=0; n<13; n++){
numbers=(int)string[i+n];
numbers=numbers-48;
interm=interm * numbers;
}
return interm;
}
You didn't read the problem description carefully. The string that you are printing is not what the problem is asking for. Your code looks like it finds the solution to the problem but doesn't print it.
#include<iostream>
#include<string>
using namespace std;
void main(){
int max=882;
int num=0;
int strnum[1000],N;
char ch[100];
FILE *fp=fopen("problem8.txt","r");
while(fgets(ch,100,fp)){
for(int i=0;i<50;i++)
strnum[num+i]=ch[i]-'0';
num+=50;
}
fclose(fp);
for(int i=0;i<995;i++){
N=strnum[i]*strnum[i+1]*strnum[i+2]*strnum[i+3]*strnum[i+4];
if(max<N)
max=N;
}
printf("%d\n",max);
}
from here

Resources