Floating point exception: 8 - c

Why am I getting the error: Floating point exception: 8
#include<stdio.h>
//grid problem
int fact(int n)
{
int i,f=1;
if(n==0)
return 1;
for(i=1;i<=n;i++)
f*=i;
return f;
}
int uniquePaths(int A, int B) {
float m;
m=fact(A+B-2)/(fact(A-1)*fact(B-1));
return m;
}
int main(int argc, char const *argv[])
{
int a,b;
//aXb grid
scanf("%d%d",&a,&b);
printf("%d\n",uniquePaths(a,b) );
return 0;
}

If you add pre- and postconditions using the function assert you can make sure that parameters and function results have reasonable values:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
//grid problem
int fact(int n)
{
assert(n >= 0);
int i, f = 1;
if (n == 0) {
return 1;
}
for (i = 1; i <= n; i++) {
f *= i;
}
assert(f >= 1);
return f;
}
int uniquePaths(int A, int B)
{
assert(A >= 1);
assert(B >= 1);
int q = fact(A - 1) * fact(B - 1);
assert(q > 0);
int m = fact(A + B - 2) / q;
assert(m >= 1);
return m;
}
int main(int argc, char const *argv[])
{
int a, b;
//aXb grid
int n = scanf("%d%d", &a, &b);
if (n == 2) {
printf("%d\n", uniquePaths(a, b));
} else {
fprintf(stderr, "invalid input\n");
exit(1);
}
return 0;
}
On my machine, running the program above with intput 10 10, for instance, will result in
t: t.c:16: int fact(int): Assertion `f >= 1' failed.
Aborted
(I don't know why you get a floating point exception however.)

Related

Assert on Arrays

#include<stdio.h>
#include<assert.h>
const char *authour ="Alexandre Santos";
int ints_get(int *a)
{
int result = 0;
int x;
while (scanf("%d", &x) !=EOF)
{
a[result++] = x;
}
return result;
}
int sum_odd(const int *a, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
if(i%2 != 0)
sum += a[i];
return sum;
}
int sum_all(const int *a, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum = sum + a[i];
return sum;
}
int final(const int *a, int n)
{
return sum_all(a,n) - sum_odd(a,n);
}
void unit_test_even_values_minus_odd_values(void){
int a1[8] = {1,2,3,4,5,6,7,8};
assert(final(a1, 8) == 4);
assert(final(a1, 6) == 3);
assert(final(a1, 4) == 2);
assert(final(a1, 2) == 1);
}
void unit_tests(void){
unit_test_even_values_minus_odd_values();
}
void test_sum(void)
{
int a[100];
int n = ints_get(a);
int total = final(a,n);
printf("%d\n", total);
}
int main()
{
test_sum();
return 0;
}
I have this program but I don't understand how the assertions work here, my main question is what the second number represents. For example I understand that assert(final(a1, 8) == 4) I understand that a1 is the array determined in the line above but I can't understand the second number (8).... Can anyone explain to me how this works? I tried to search a little bit but I still don't understand...
The second argument to final is the number of values to work on from that array, starting from the beginning.
final(a1, 8) sums all eight values. final(a1, 6) only sums the first six values.

C.find an error in a very small program.Calculating the sum of the first k numbers of the sequence

Calculating the sum of the first k numbers of the sequence a[0] = 1, a[k] = k*a[k-1] +1/k ( k = 1, 2, ... ).
UPD
There is still a problem with the recursive function ...What is the error?
#include <stdio.h>
#include <stdlib.h>
float m(float n){
float k=1;
float sum=k;
int i;
for (i=1; i<n;i++){
k = (i*k+1.0/i);
sum = sum+k;
}
return sum;
}
float Fn(float n)
{
if (n==0) {
return 1;}
return ((n*Fn(n-1)+1.0/n)+Fn(n-1));
}
int main(int argc, char *argv[]) {
float k;
printf("input k : ");
scanf("%f",&k);
printf("res %f \n",Fn(k));
return 0;
}
There were several issues in your code:
Integer division: 1/n = 0
There was a confusion between the term value Fn and the sum value
An iterative solution is simpler here than a recursive one
Here is a code, with both iterative and recursive implementations:
#include <stdio.h>
#include <stdlib.h>
float sum_fn(int n){
float Fk = 1;
float sum = Fk;
for (int i = 1; i <= n; i++){
Fk = i*Fk + 1.0/i;
sum += Fk;
}
return sum;
}
float sum_recursive(int n, float *sum){
if (n == 0) {
*sum += 1.0;
return 1.0;
}
float Fn = n * sum_recursive(n-1, sum) + 1.0/n;
*sum += Fn;
return Fn;
}
int main(int argc, char *argv[]) {
int k;
printf("input k : ");
scanf("%d", &k);
printf("k = %d\tsum = %f\n", k, sum_fn(k));
float sum = 0;
sum_recursive(k, &sum);
printf("k = %d\tsum = %f\n", k, sum);
return 0;
}

I need print out up to the N'th term and the N'th term give given that 1, 2, 5, 26, 677,.

given that 1, 2, 5, 26, 677,...
such that the nth term of the series equals to
(n-1)th ^2 +1 and the first term of the series is 1.
Write a program using recursive function named f to compute the nth term.
I can't get anything to print out. How can i Fix this?
#include <stdio.h>
#include <math.h>
int f(int n)
{
if(n==1)
{
return 1;
}
else
{
return f((n-1)^2)+1;
}
}
int main()
{
int N,i;
printf("Enter the number of terms: ");
scanf("%d",&N);
int u = f(N);
for(i=0;i<N;i++)
{
printf("%d ",u);
}
return 0;
}```
The most direct way (IMO) to do this is something like:
#include <stdio.h>
#include <stdlib.h>
long
f(int n)
{
return n == 1 ? 1 : f(n-1) * f(n-1) + 1;
}
int
main(int argc, char **argv)
{
int n = argc > 1 ? strtol(argv[1], NULL, 10) : 2;
for(int i=1; i < n; i++) {
printf("%d: %ld\n", i, f(i));
}
return 0;
}
There are a lot of improvements that should be made above, but that gives the basic idea. But it seems like you're trying to do something more along the lines of:
#include <stdio.h>
#include <stdlib.h>
long
f(int n)
{
long r= 1;
if(n > 1) {
r = f(n-1);
r = r * r + 1 ;
}
printf("%d: %ld\n", n, r);
return r;
}
int
main(int argc, char **argv)
{
int n = argc > 1 ? strtol(argv[1], NULL, 10) : 2;
f(n);
return 0;
}
You should probably include some error checking to prevent overflow and return non-zero from main if that happens, but that will depend on how you want to handle overflow.

Segmentation fault (core dumped) in C that includes pointers

I am working on a homework project and we are using pointers and it seems that those pointer values are changed at some in my program and I can't find that specific spot. rp is the pointer that is changing. I am new to pointers so I tried de dereferencing them in different ways, but I am ultimately unsure.
Here is my code
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int getMenuChoice();
void generateCoords(int row, int cols, char[][cols], int *rp, int *cp);
int main() {
int myChoice;
int userDifficulty;
srand(time(NULL));
do {
myChoice = getMenuChoice();
if (myChoice == 1) {
printf("\nEnter difficulty (1, 2, or 3): ");
scanf("%d", &userDifficulty);
int gridSize = 2;
gridSize = userDifficulty * 2;
char array[gridSize][gridSize];
preSetBoard(gridSize, gridSize, array);
generateCoords(gridSize, gridSize, array, x, y);
//printf("%d %d", x, y);
}
} while (myChoice != 0);
return 0;
}
int getMenuChoice() {
int userInput;
printf("***MEMORY!***\n");
printf("1 - Play Game\n");
printf("2 - Check Scores\n");
printf("0 - EXIT\n");
scanf("%d", &userInput);
return userInput;
}
void generateCoords(int row, int cols, char array[][cols], int *rp, int *cp) {
_Bool run = 1;
srand(time(NULL));
while (run) {
int place1 = rand() % (row) + 1;
rp = &place1;
//printf("%d\n", *rp);
int place2 = rand() % (row) + 1;
cp = &place2;
//printf("%d\n", *cp);
if (array[*rp][*cp] == 'A') {
run = 0;
}
}
}
The expected output is a number between 1 and 3.
There are multiple issues in the posted code:
You add 1 to the random numbers generated for coordinates: this may cause the program to try and access the 2D array beyond its boundaries. Index values should be >= 0 and < size.
Furthermore, you do not pass addresses of int variables to generateCoords, but x and y which are not defined within the code posted. This does not really pose a problem since you modify the function arguments to point to local variables.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int getMenuChoice(void);
void presetBoard(int row, int cols, char[][cols]);
void generateCoords(int row, int cols, char[][cols], int *rp, int *cp);
int main() {
int myChoice, userDifficulty, x, y;
srand(time(NULL));
while ((myChoice = getMenuChoice()) > 0) {
if (myChoice == 1) {
printf("\nEnter difficulty (1, 2, or 3): ");
if (scanf("%d", &userDifficulty) != 1)
break;
int gridSize = userDifficulty * 2;
char array[gridSize][gridSize];
preSetBoard(gridSize, gridSize, array);
generateCoords(gridSize, gridSize, array, &x, &y);
printf("%d %d\n", x, y);
}
}
return 0;
}
int getMenuChoice(void) {
int userInput;
printf("***MEMORY!***\n");
printf("1 - Play Game\n");
printf("2 - Check Scores\n");
printf("0 - EXIT\n");
if (scanf("%d", &userInput) != 1)
userInput = 0;
return userInput;
}
void generateCoords(int rows, int cols, char array[][cols], int *rp, int *cp) {
int col, row;
for (;;) {
row = rand() % rows;
col = rand() % cols;
if (array[row][col] == 'A') {
break;
}
}
/* return found coordinates to the caller */
*rp = row;
*cp = col;
}

Calculating power raised to a number

I am calculating the power of x raised to n. I can't understand one thing: why is it showing segmentation fault when I am both declaring and initializing the temp variable at the start? I do know what segmentation fault is, but why is it showing.
#include<stdio.h>
int power(int x,unsigned int y)
{
int temp=power(x,y/2);
if(y==0)
return 1;
if(y%2==0)
return temp*temp;
else
return x*temp*temp;
}
//Driver function
int main(int u, int v)
{
printf("Enter the value of u and v");
scanf("%d %u",&u,&v);
printf("%d",power(u,v));
return 0;
}
You will recurse infinitely. You need a small adjustment [please pardon the gratuitous style cleanup]:
#include <stdio.h>
int
power(int x, unsigned int y)
{
//int temp = power(x, y / 2);
if (y == 0)
return 1;
int temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
// Driver function
int
main(int argc,char **argv)
{
int u;
unsigned int v;
printf("Enter the value of u and v");
scanf("%d %u", &u, &v);
printf("%d\n", power(u, v));
return 0;
}

Resources