Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to compute the largest of the sums of numbers that appear on the paths of a triangle of numbers starting from the top towards the base so that on each path the next number is located on the row below, more precisely either directly below or below and one place to the right.
Getting a Segmentation fault.
Don't know why??
#include<stdio.h>
#include<stdlib.h>
int func(int **arr, int n, int i,int max)
{
int j,m,a,b,c;
if(i==(n-1))
return max;
else
{
for(j=0;j<=i;j++)
{
a=*(*(arr+i)+j);
b=*(*(arr+(i+1))+j);
c=*(*(arr+(i+1))+(j+1));
if(((a+b)>=(a+c))&&((a+b)>max))
{
max=(a+b);
m=j;
}
if(((a+c)>(a+b))&&((a+c)>max))
{
max=(a+c);
m=j+1;
}
}
*(*(arr+(i+1))+m)=max;
func(arr,n,i+1,max);
}
}
int main()
{
int n,array[100][100],i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
scanf("%d",&array[i][j]);
}
printf("%d\n",func(array,n,0,0));
return 0;
}
Because you are passing an int array[100][100] to an int ** and they are not interchangeable:
int func(int **arr, int n, int i,int max)
Must be:
int func(int (*arr)[100], int n, int i,int max)
Take a look to this related question.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not able to understand where I am going wrong. Please help! I am new to the website. Appreciate all the help. Thanks a lot :D
#include <stdio.h>
int main()
{
printf("Hello World")
}
int factorial(int x) {
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int a = 9;
int b;
b = factorial(int a);
printf("%i", b);
I have corrected the code and added some comments. I also rearranged the factorial slightly, so that it works for 0! which is 1.
#include <stdio.h>
int factorial(int x) { // added the argument type int
int product = 1; // use another variable
for(int i = 2; i <= x; i++) {
product *= i;
}
return product;
}
int main()
{
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
Note that you can only generate up to 12! and after that you get overflow due to the range of a 32-bit int.
First here printf("Hello World") you are missing ;
Second add this part to your main.
int main()
{
printf("Hello World");
int a = 9;
int b;
b = factorial(a);
printf("%i", b);
}
and when you are calling your function in main,you shouldn't send int a to function b = factorial(int a) ,because by saying int a instead of a you are redefining it.(so it will be uninitialized,if redefinition is not error)
also as said in comments you should add a prototype for factorial before main or move it before main.
Finally your loop in factorial is infinitive ,for(i=1; i < x; i++) since you're doing x *= i;
this condition i < x is never true.
you will increase x until int type has not enough space for it. so a garbage value will be assigned to it ,and you will exit the loop.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So i tried to code a function to output nCr (the combinations of choosing k elements from n elements) but it does not show any output...
I think I am unable to call the function correctly but I think my syntax is correct:
#include <stdio.h>
int factorial( int n)
{
int i, nff, nf[10];
for(i=0;i<n;i++)
nf[i]=(n-i);
for(i=0;i<n-1;i++)
nf[i+1]*=nf[i];
nff=nf[n-1];
return nff;
}
int faktorial( int k){
int i, kff, kf[10];
for(i=0;i<k;i++)
kf[i]=(k-i);
for(i=0;i<k-1;i++)
kf[i+1]*=kf[i];
kff=kf[k-1];
return kff;
}
int facktorial( int k, int n){
int i, nkff, nkf[10];
for(i=0;i<(n-k);i++)
nkf[i]=(n-k)-i;
for(i=0;i<(n-k)-1;i++)
nkf[i+1]*=nkf[i];
nkff=nkf[(n-k)-1];
return nkff;
}
int combination( int k, int n)
{
// this function shall call (make use of) another function factorial()
int nfa,kfa,nkfa,nCra;
nfa=factorial(n);
kfa=faktorial(k);
nkfa=facktorial(k,n);
nCra = nfa/(kfa*nkfa);
return nCra;
}
int main(void)
{
int n, k, nCr;
scanf("%d %d", &n, &k);
nCr=combination (k, n);
return 0;
}
You just need to output the result when it is returned:
printf("%d\n", nCr);
return 0;
Another issue, your program will crash if the input is 0 or a number greater than 10, it is better not to use an array to computer factorial.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
int cauta(const void *x, int n, int dim_el, const void *el)
{
char *c = (char*) x;
int i;
for(i = 0; i < n; i++)
{
if(memcmp(c + i * dim_el, el, dim_el) == 0)
return 1;
}
return -1;
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int k;
k = cauta(a, sizeof(a) / sizeof(a[0]), sizeof(a[0]), a[0]);
printf("%d", k);
return 0;
}
The problem appears on the commented line. The function returns 1 if "el" exists in the "x" array . It's a simple, yet I don't understand exactly why it's a segmfault.
Also, this is the call stack display when I tried debugging it line by line.
In your code, the function parameters are
int cauta(const void *x,int n,int dim_el,const void *el)
where el expects a const void *, whereas, while calling,
cauta(a,sizeof(a)/sizeof(a[0]),sizeof(a[0]),a[0]);
you passed a[0] which is an int.
You need to pass an address, like &a[3], for example.
That said, int main() should be int main(void) to conform to the standards.
You're passing in an int i.e. a[0] into cauta as an const void * that's going to cause the error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
The quicksort code below is giving incorrect results.Could anybody tell what is wrong?
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
int b[MAX];
void print(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
int party(int a[],int p,int q)
{
int pivot=a[p];
int t=a[p];
a[p]=a[q];
a[q]=t;
int i,j,k,store=p;
for(i=p;i<=q-1;i++)
{
if(a[i]<=pivot)
{
t = a[i];
a[i] = a[store];
a[store] = t;store++;}
}
t = a[store];
a[store] = a[q];
a[q] = a[store];
return store;
}
void quicksort(int a[],int p,int q)
{
if(p>=q)
{
return;
}
int r=party(a,p,q);
quicksort(a,p,r-1);
quicksort(a,r+1,q);
}
int main()
{
printf("Enter No. oF elements for sorting.\n");
int i,j,k,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Element %d\t",i+1); scanf("%d",&b[i]);
}
print(b,n);
quicksort(b,0,n-1);
print(b,n);
return 0;
}
EDIT: Just spent few minutes and did some basic indentation instead of asking author to do it. I could have put proper variable names but since answerd felt fine.
Buried in that dreadful code formatting you seem to like is this:
t=a[store];a[store]=a[q];a[q]=a[store];
Which to sane people looks like this:
t=a[store];
a[store]=a[q];
a[q]=a[store];
You're setting a[q] to the same value it just was, and not swapping anything. It should read:
t=a[store];
a[store]=a[q];
a[q]=t;
Btw, you don't need both low and high indices, you only need a base array address and a length for this algorithm (and a little pointer math, of course):
static void swap_int(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void quicksort(int a[], int len)
{
int i=0, pvt=0;
if (len <=1)
return;
for (;i<len;++i)
{
if (a[i] < a[len-1])
swap_int(a+i,a+pvt++);
}
swap_int(a+pvt,a+len-1);
quicksort(a, pvt++);
quicksort(a+pvt, len-pvt);
}
There ya go, It even has the partitioning built in. The pivot should be random-selected, but thats for another day, I suppose.
This was a very silly mistake.
In swapping a[store] and a[q] in the party function :
t=a[store]; a[store]=a[q]; a[q]=a[store]; -----> This is Wrong.
a[q]= t should be the last statement.
Now it works fine. Thanks for the super quick response anyways.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm wondering where I've gone wrong here. There following is my matrix multiplication function.
void matMul(Matrix A, Matrix B, int ARows, int ACols, int BCols, Matrix C){
int x,y,z;
for(x=0; x<ARows; x++){
for(y=0; y<BCols; y++){
for(z=0; z<ACols; z++){
C[x][y]+=(A[x][z])*(B[z][y]);
}
}
}
} /* matMul */
However, it is not doing its job of multiplying matrices together. Can Anyone spot where I seemed to have messed up?
You need to have entries of C be set to 0. Either you do this when you initialize the object or, if it is too expensive to do that there (in terms of code) you move them to the matrix multiplication:
void matMul(Matrix A, Matrix B, int ARows, int ACols, int BCols, Matrix C){
int x,y,z;
for(x=0; x<ARows; x++){
for(y=0; y<BCols; y++){
C[x][y] = 0;
for(z=0; z<ACols; z++){
C[x][y]+=(A[x][z])*(B[z][y]);
}
}
}
} /* matMul */