I have a program that calculates the factorial of a number (even over 20), but views the number as a series of digits. In this way:
Here's code:
#include <stdio.h>
#include <stdlib.h>
#define MAXCIF 1000
int main()
{
int f[MAXCIF] = { 1 };
int i, j, n, pom, transmission;
do
{
printf("n="); scanf("%d", &n);
} while (n < 1 || n > 100);
for (i = 2; i <= n; i++)
{
for (j = 0,transmission=0; j < MAXCIF; j++)
{
pom = f[j] * i + transmission;
f[j] = pom % 10;
transmission = pom / 10;
}
}
printf("%d != ", n);
for (j = MAXCIF - 1; f[j] == 0; j--); // It will skip leading zeros
for (; j >= 0; j--)
printf("%d", f[j]); // Prints digits
return 0;
}
What makes me a problem is that I don't understand how these two loops work
for (i = 2; i <= n; i++)
{
for (j = 0,transmission=0; j < MAXCIF; j++)
{
pom = f[j] * i + transmission;
f[j] = pom % 10;
transmission = pom / 10;
}
}
For example, take n = 2:
As I understand these loops, pom = 2, f[0] = 2, transmission = 0.
In the next iteration j = 1, then how is f[1] = 0?
If someone could explain these two for loops, that is, how to manually understand what is happening in them, I would be grateful.
Related
The code fits the first number and prints it constantly. how can i fix this?
int count = 0;
for (int i = 0; i <= 20; i++) {
for (count = 2; i > 1; count++) {
while (i % count == 0) {
printf("%d ", count);
i = i / count;
}
}
}
The values in each iteration are as follows.
count = 0; i = 0; Doesn't enter the second for.
count = 0; i = 1; Doesn't enter the second for.
count = 0; i = 2; Enters the second for. count = 2;
2 % 2 == 0 - Enters the while.
i = 2 / 2; 1 % 2 == 1; Doesn't enter the while.
Back to the second for - count = 3;, i = 1; Doesn't enter the second for.
Back to the first for - i < 20;, so i = 2.
count = 2; i = 2; and we are back to step 4, with an infinite loop.
This might be what you are looking for -
int j, count = 0;
for (int i = 20; i > 0; i--)
{
printf("\n%d: ", i);
for(count = 2, j = i; j > 1; count++)
{
while(j % count == 0)
{
printf("%d ", count);
j = j / count;
}
}
}
Define a function that checks whether a given number n is prime:
bool is_prime(int n)
{
if (n < 2) return false;
for (int i = 2; i <= n/i; ++i) // Doing i*i<=n may overflow
if (n % i == 0) return false;
return true;
}
And then call it like:
for (int i = 0; i <= 20; i++)
if(is_prime(i))
printf("%d\n", i);
Or more directly (i.e. without a function):
int main(void)
{
int mark;
for (int n = 2; n <= 20; n++) {
mark = 1;
for (int i = 2; i*i <= n; ++i)
if (n % i == 0) mark = 0;
if (mark) printf("%d\n", n);
}
}
My task is: If we look at any two neighbour values in an array, if the one on the right is two times greater than the one on the left, their average should be inserted between them and the new array consisting of old and new elements should be printed. I have a problem with moving the other elements after average.And using special functions or libraries is not allowed.I am beginner, and I hope you could help.
#include <stdio.h>
int main() {
int n, i, j;
double a[100], average;
printf("Enter the number of elements: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf", &a[i]);
}
for (i = 0; i < n; i++) {
if ((a[i + 1] / a[i]) == 2) {
for (i = j = 0; i < n; ++i)
b[j++] = a[i];
if (a[i + 1] / a[i] == 2)
average = (a[i + 1] + a[i]) / 2;
b[j++] =average;
}
}
for (i = 0; i < j; ++i) {
printf("%lf\n", b[i]);
}
}
A simple way to solve your problem is adding double b[199];, and copying everything over:
for (i = j = 0; i < n; ++i) {
b[j++] = a[i];
if (...) b[j++] = ...; /* Append the average to b. */
}
for (i = 0; i < j; ++i) {
printf("%lf\n", b[i]);
}
If you really want to move the elements forward within a itself, then you can do it by adding an inner for loop (and an additional loop variable int k;) which copies the elements one-by-one:
for (k = n++; k > i; --k) {
a[k] = a[k - 1];
}
In order to insert an element in an array, you must copy the elements with higher index from the last one down.
Also avoid dividing by a[i] that can be zero, and properly handle 0,0 that match the criteria for inserting the average, and skip the inserted value to avoid inserting more zeros.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, j;
printf("Enter the number of elements: ");
if (scanf("%d", &n) != 1 || n <= 0)
return 1;
double *a = malloc(sizeof(*a) * (2 * n - 1)); // allocate the array to the maximum size
if (a == NULL)
return 0;
for (i = 0; i < n; i++) {
if (scanf("%lf", &a[i]) != 1)
return 1;
}
for (i = 1; i < n; i++) {
if (a[i] == a[i - 1] * 2) {
for (j = n; j > i; j--)
a[j] = a[j - 1];
a[i] = (a[i - 1] + a[i]) / 2;
n++; // increase number of elements
i++; // skip the new value
}
}
for (i = 0; i < n; ++i) {
printf("%f\n", a[i]);
}
free(a);
return 0;
}
To insert an element in a specific position you would need to move the rest of the array. However doing it many times is expensive and you may prefer to use an array to store the position at which you want to insert the elements and then insert them all at once.
Alternatively you can create a new array where to copy the original plus the new values.
However there's an easier and faster way, that is adding the new values straight away, while you fill the original array. Here's a program that does that.
#include <stdio.h>
#define SIZE 100
int main() {
int i, n, avg = 0;
double a[SIZE];
while( puts("Enter the number of elements:") && (scanf("%d", &n) != 1 || n < 1 || n > SIZE) );
scanf("%lf", &a[0]);
for(i = 1; i < n+avg && i < SIZE-1 && scanf("%lf", &a[i]) == 1; i++) {
if( a[i] == a[i-1] * 2 ) {
a[i+1] = a[i];
a[i] = (a[i] + a[i-1]) / 2;
++avg;
++i;
}
}
for(i = 0; i < n+avg; i++) {
printf("%lf\n", a[i]);
}
return 0;
}
How do I get to write to 2D pointers where I have pnumber[2%4][2%4] and how can I get pnumber with more than 3 ciphers to be displayed?
I'm making a program to write pascals triangle in C.
When the pointer pnumbers[i][j] have both i and j = 2 mod 4, except for when i and j = 2, then my program won't write to the address and give the error message:
pascals triangle: malloc.c:2406: sysmalloc: Assertion '{old_top == initial_top (av) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =7;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
/*
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
*/
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
When I avoid writing to these addresses and just print them out I get some seemingly random integer at these memory addresses.
Also when avoid these addresses and just print out so many rows that I get some spots with a higher integer with more than 3 siphers, it seems to overflow - and I don't see the logic behind it.
The result of running the second code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =20;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
/*
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
*/
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
But row number 13 is still quite messed up.
Code is experiencing int overflow and thus undefined behavior (UB).
With 32-bit int and int factorial(int p), p > 12 oveflows the int range.
Code could use a wider integer type (long long works up to p==20), but improvements can be made at NchooseM() to avoid overflow for higher values.
Something like the below. Works up to int n = 30;
int NchooseM(int n, int m) {
// return factorial(n)/(factorial(n-m)*factorial(m));
int nm = 1;
int den = 1;
for (int i = m+1; i <= n; i++) {
assert(INT_MAX/i >= nm);
nm *= i;
assert(nm % den == 0);
nm /= den++;
}
return nm;
}
Tried unsigned long long and works up to int n = 62;
Edit: Another bug:
I "fixed" by initializing all to 1, yet I suspect something remains amiss in /* Calculating the value of pnumbers[k][l] */ for (i = 0; i < n; i++) { code.
pnumbers[i] = malloc((i + 1) * sizeof pnumbers[i][0]);
for (int j = 0; j < i + 1; j++) {
pnumbers[i][j] = 1;
}
Aside: rather than pnumbers[i] = (int *) malloc((i+1) * sizeof(int));, consider below with no unneeded cast nor trying to match the right type.
pnumbers[i] = malloc(sizeof pnumbers[i][0] * (i+1));
Im trying to submit a solution for the problem FCTRL2(https://www.codechef.com/problems/FCTRL2) on codechef. On executing the code it sometimes gets executed successfully while sometimes it gives a SIGSEGV error. But when I submit it, it always shows wrong answer. Though the code gives correct answer when i run it on any other IDE.
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
int testCases, i, j, k, n, num, digits, carry = 0, temp;
scanf("%d", &testCases);
int testArr[160];
for (i = 0; i < testCases; i++)
{
scanf("%d", &n);
num = n;
if (n == 0 || n == 1)
{
testArr[0] = 1;
digits = 1;
}
else
{
k = 0;
for (j = 10; n != 0; j = j * 10)
{
testArr[k] = n % j;
n = n / j;
k++;
}
digits = k;
for (j = 1; j < num; j++)
{
for (k = 0; k < digits; k++)
{
temp = testArr[k] * j + carry;
if (temp > 10)
{
testArr[k] = temp % 10;
carry = temp / 10;
}
else
{
testArr[k] = temp;
carry = 0;
}
}
if (carry > 10)
{
testArr[k] = carry % 10;
k++;
testArr[k] = carry / 10;
digits = k + 1;
carry = 0;
}
else if (carry > 0)
{
testArr[k] = carry;
digits = k + 1;
carry = 0;
}
}
}
for (k = (digits - 1); k >= 0; k--)
{
printf("%d", testArr[k]);
}
printf("\n");
}
return 0;
}
#include<stdio.h>
#define max 2000
int arr1[max], arr2[max], arr3[max];
void fib();
int main()
{
int num, i, j, flag = 0;
for(i = 0; i<max; i++)
arr1[i] = arr2[i] = arr3[i] = 0;
arr2[max - 1] = 1;
printf("Enter the term : ");
scanf("%d", &num);
for(i = 0; i<num; i++)
{
fib();
if(i == num - 3)
break;
for(j = 0; j<max; j++)
arr1[j] = arr2[j];
for(j = 0; j<max; j++)
arr2[j] = arr3[j];
}
for(i = 0; i<max; i++)
{
if(flag || arr3[i])
{
flag = 1;
printf("%d", arr3[i]);
}
}
getch();
return 1;
}
void fib()
{
int i, temp;
for(i = 0; i<max; i++)
arr3[i] = arr1[i] + arr2[i];
for(i = max - 1; i>0; i--)
{
if(arr3[i]>9)
{
temp = arr3[i];
arr3[i] %= 10;
arr3[i - 1] += (temp / 10);
}
}
}
The above code generates the nth Fibonacci number. I am not able to understand how this works. Basically the Fibonacci number get stored in a very large array arr3[].
Please explain the logic involved in this code.
How does the fib() function work as well?
Here is a simple Fibonacci loop.
#include <stdio.h>
int main()
{
int term = 20, last2=0, last1=1, fib, i;
for (i=0; i<term; i++) {
fib = last2 + last1;
last2 = last1;
last1 = fib;
}
printf ("Term %d = %d\n", i, fib);
return 0;
}
Program output:
Term 20 = 10946
Although there is more than one idea as to where the sequence starts.
The example code in the original post is dealing with large numbers by storing 1 decimal digit per element in each of the arrays. It initializes arr[3] = arr2[] = arr1[] = 0, then arr2[] = 1. In the loop, fib() performs one instance of arr3[] = arr1[] + arr2[], handling the carries, then the loop does arr[1] = arr2[], arr2[] = arr3[]. If num < 3, the for loop exits on the loop condition i < num, if n >= 3, the loop exit when i == (num-3). (This could be avoided). The print loop skips leading zeroes in arr3[], setting flag once a non-zero value is found. The code needs some minor fixes. Here is a fixed example. Note that getch() may be _getch() in some environments (from conio.h). The second example below only uses two arrays. Fibonacci numbers starting from 0 are 0 1 1 2 3 5 8 ...
#include <conio.h>
#include <stdio.h>
#define max 2000
int arr1[max], arr2[max], arr3[max];
void fib();
int main()
{
int num, i, j;
for(i = 0; i<max; i++)
arr1[i] = arr2[i] = arr3[i] = 0;
arr1[max - 1] = 1;
printf("Enter the term : ");
scanf("%d", &num);
for(i = 0; i<num; i++)
{
fib();
for(j = 0; j<max; j++)
arr1[j] = arr2[j];
for(j = 0; j<max; j++)
arr2[j] = arr3[j];
}
for(i = 0; i < max-1; i++)
if(arr3[i])
break;
for( ; i < max; i++)
printf("%d", arr3[i]);
getch();
return 0;
}
void fib()
{
int i, temp;
for(i = 0; i<max; i++)
arr3[i] = arr1[i] + arr2[i];
for(i = max - 1; i>0; i--)
{
if(arr3[i]>9)
{
temp = arr3[i];
arr3[i] %= 10;
arr3[i - 1] += (temp / 10);
}
}
}
This example only uses two arrays, by alternating which array contains the sum (a1 += a0, a0 += a1). It uses Duff's device to enter the loop. Since the largest sum from digit + digit + carry is < 20, the carry loop in fib() was simplified.
#include <conio.h>
#include <stdio.h>
#define max 2000
void fib(unsigned char *a0, unsigned char *a1);
int main()
{
unsigned char a0[max], a1[max];
size_t i;
int n;
printf("Enter the term : ");
scanf("%d", &n);
for(i = 0; i < max; i++)
a0[i] = a1[i] = 0;
a0[max-1] = n & 1; /* if n even, a0=0=fib(0), a1=1=fib(-1) */
a1[max-1] = 1 - a0[max-1]; /* if n odd, a1=0=fib(0), a0=1=fib(-1) */
switch(n&1){
do{
fib(a0, a1);
case 1:
fib(a1, a0);
case 0:
continue;
}while(0 <= (n -= 2));
}
for(i = 0; i < max-1; i++)
if(a0[i])break;
for( ; i < max; i++)
printf("%d", a0[i]);
getch();
return 0;
}
void fib(unsigned char *a0, unsigned char *a1)
{
size_t i;
for(i = 0; i < max; i++)
a1[i] += a0[i];
for(i = max - 1; i > 0; i--){
if(a1[i] >= 10){
a1[i] -= 10;
a1[i-1] += 1;
}
}
}
Here's a much better implementation of the Fibonacci series
#include<iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}