#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Related
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
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);
}
Decimal to octal conversion c.using code blocks 16.01.the conversion works until 63.This is my first time posting here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// conversion of decimal to octal
int main()
{
int num,i,len=0,x[100];
printf("enter a number\t");
scanf("%d",&num);
while (num>0){
x[len]=num%8;
num=num/8;
len++;
}
for(i=len;i>=0;i--)
printf("\n%d",x[i]);
return 0;
}
This will work for the conversion from decimal to octal.
Note: Check the logic works!.Make the sum long int. After 63 your int no longer able to hold the answer.
The problem with the code is actually reported by the compiler if you turn on some warnings, however the problem is that you are implicitly converting from double to int, and depending on the implementation, you'd lose precision. pow returns a double but you're storing it in an int (x)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
/* ^^^^^^^^^^^^^^^^ problem here */
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
now the fix
Instead of using the library pow, you should implement your own that's integer only (because you KNOW you are using ints only)
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
Now to clean up the program a bit:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long pow10(int n);
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
int main()
{
int num, count;
long sum, x;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow10(count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%ld",sum);
return 0;
}
This question already has an answer here:
C: gets() skips the first input [closed]
(1 answer)
Closed 6 years ago.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n,i;
char a[10][100];
printf("\n Enter the no. of strings:");
scanf("%d",&n);
printf("\n enter the %d numbers:",n);
for(i=0;i<n;i++)
{
printf("\n %d",i);
gets(a[i]);
}
for(i=0;i<=n;i++)
{
puts(a[i]);
}
return 0;
}
If n = 3 then it takes only two strings at index 1 and 2 it skips 0, why doesn't it take input at 0 ?
Here a is my array of strings.
The reason for wrong behavior is that scanf does not read the ENTER which is necessary to confirm the input of n. If you add dummy call of gets it does:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n,i;
char a[10][100];
printf("\n Enter the no. of strings:");
scanf("%d",&n); gets(a[0]);
printf("\n enter the %d numbers:",n);
for(i=0;i<n;++i)
{
printf("\n %d",i);
gets(a[i]);
}
for(i=0;i<n;++i)
{
puts(a[i]);
}
return 0;
}
Please diff my version with the original one. I did fix another issue in the output loop.
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;
}