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 */
Related
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 2 years ago.
Improve this question
double fracsum (int a, int b, int c, int d){
float sum = 0;
int i;
for (i = 0; i < a; i++) {
sum += a;
}
return sum;
}
int main(void)
{
printf("%.3f %.3f %.3f\n",
fracsum(1,2,2,4),
fracsum(1,4,1,8),
fracsum(4,3,5,6));
return 0;
}
Did you mean:
float fracsum (float a, float b, float c, float d) {
return (a / b + c / d);
}
However the problem was maybe that you cannot divide int variables, you have to use float as argument type..
It's not clear to me exactly what you want. But I'd take a wild-elbow guess
that it's fracsum=(a/b)+(c/d). And if that's indeed what you want, then
double fracsum (int a, int b, int c, int d) {
return ( ((double)a)/((double)b) + ((double)c)/((double)d) ); }
Couldn't be much easier.
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 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.
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 9 years ago.
Improve this question
I'm trying to program Composite Simpson's Rule in C. The formula is:
where x_j=a+jh for j=0, 1, ..., n-1, n with h=(b-a)/n; in particular, x_0=a and x_n=b.
For some reason, the first and second loop have the same value. I checked over this a lot of times, but I can't seem to find my mistake.
#include <stdio.h>
#include <math.h>
float f(float);
float a;
float b;
float x;
float h;
int n;
int j;
a=0;
b=2;
n=8;
h = (n - j) / b;
float first;
float second;
int main() {
sum = (h / 3.0f) * (f(h) + f(n));
printf("%f\n", sum);
second = (4.0f) * h * f(a);
}
printf("second sum: %f\n",second );
sum = sum + first + second;
printf("%f\n", sum);
return 0;
}
The answer should be around 3.1 (The value of the final sum)
Your divisions won't probably do what you expect:
(2 / 3) == 0
Dividing int with int will result in int.
Use float constants (2.0f / 3.0f)
Edit:
You still have the same problem with the other n / 2.
And you should use %f when printing floats: printf("first sum: %f\n",first);
The value of the integral is: 3.109337