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.
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 last year.
Improve this question
#include <stdio.h>
int value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
int value(int *a){
int c = (*a)*10;
return c;
}
In this code, I transfer the address in function but it does not change, Why?
You have 2 ways of having a function change data outside it: you can pass a variable by reference and update it, or you can return a value and use that. You are mixing half of each method instead. I reordered the code to make my commentary clearer.
#include <stdio.h>
int value(int *a);
// here you are passing by reference, good
int value(int *a){
int c = (*a)*10; // but you don't change a
return c; // instead you return a new value
}
int main(){
int num = 4;
value(&num); // but here you ignore the new value.
printf("value of number is = %d", num);
return 0;
}
to make reference way work, you need:
#include <stdio.h>
void value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
void value(int *a){
*a = (*a)*10; // change the value that a points at
// no need to return anything
}
or to make the return way work:
#include <stdio.h>
// no need to pass by reference
int value(int a);
int main(){
int num = 4;
num = value(num); // use value return by function
printf("value of number is = %d", num);
return 0;
}
int value(int a){
int c = a * 10;
return c;
}
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 5 years ago.
Improve this question
Inside functions static variables retain their value when called again but why it is not happening in this case ?
#include <stdio.h>
void print(void) {
static int x;
x = 10;
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}
It is giving output as 15 15.
You assign a new value each time:
static int x;
x = 10; // <- assignment
What you probably want is initialization:
static int x = 10; // <- initialization
Initialization happens only once during variable lifetime, and static means the variable lives for the whole execution time of your program.
Initializing static variable will solve the issue. Below corrected example resolves the issue and gives me 15 and 20
#include <stdio.h>
void print(void) {
static int x = 10; //Initialize here
// x = 10; //remove this statement
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}
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 5 years ago.
Improve this question
int main() {
long int a;
long int b;
long int c;
long int d;
long int e;
scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e);
int i,j;
long int sum = 0;
long int largestsum =0;
long int smallestsum = 0 ;
long int a [5] = {a,b,c,d,e};
for ( i =0;i<5;i++){
for (j = 0;j<5;j++){
if (a[j]!=a[i]){
sum+=a[j];
}
}
if (largestsum <sum){
largestsum = sum;
}
if (smallestsum>sum){
smallestsum = sum;
}
}
printf("%ld %ld",largestsum,smallestsum);
return 0;
}
i'm trying to find the largest and the smallest sums between 5 inputs the problem is that i made the array of long ints the same as variables and i have the error conflicting data types in a which is the array what is the problem ?
What is a ?
Is it a long int as in line 2 ? long int a;
Or is it an array of long int as in line 12 long int a [5] = {a,b,c,d,e};
The compiler gets confused when he sees line 12, assumes you are doing the same thing again (which it would probably tolerate), then sees that you are using a different type (array instead of long int).
He concludes "Those are not the same types. That is a conflict of types."
Solution:
Rename a-the-array to "liArray", everywhere it is referenced.
Et voila: gcc -Wall Toy.c does not complain and running it does not crash.
Afterwards read the comments, they have more to say on how to get the program actually do what it is supposed to. Below I did only the array renaming.
#include <stdio.h>
int main()
{
long int a;
long int b;
long int c;
long int d;
long int e;
scanf("%ld %ld %ld %ld %ld",&a,&b,&c,&d,&e);
int i,j;
long int sum = 0;
long int largestsum =0;
long int smallestsum = 0 ;
long int liArray[5] = {a,b,c,d,e};
for ( i =0;i<5;i++){
for (j = 0;j<5;j++){
if (liArray[j]!=liArray[i]){
sum+=liArray[j];
}
}
if (largestsum <sum){
largestsum = sum;
}
if (smallestsum>sum){
smallestsum = sum;
}
}
printf("%ld %ld",largestsum,smallestsum);
return 0;
}
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 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 7 years ago.
Improve this question
First of all, sorry for my English. I hope you will still understand what my problem is.
I am new to C programming and I am a bit confused. Here is my code.
#include <stdio.h>
#include "tableaux.h"
int main(int argc, const char * argv[]) {
int tableauUn[4] = {1, 1, 1, 1};
printf("%d\n", sommeTableau(tableauUn, 4));
printf("%d\n", moyenneTableau(tableauUn, 4));
return 0;
}
And this is the file where my functions are. I also have a file where I have my prototypes.
#include <stdio.h>
#include "tableaux.h"
int sommeTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat;
}
int moyenneTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat / taille;
}
void copierTableau(int tableau[], int taille, int tableauDeux[]) {
for (int i = 0; i < taille; i++) {
tableauDeux[i] = tableau[i];
}
}
So everything works fine. The first printf gives me the total of the values that are stored in the array and the second one gives me the average of the values.
5
1
Program ended with exit code: 0
What I don't understand is why do I get this result when I want to create a second array ?
int tableauUn[4] = {1, 1, 1, 1};
int tableauDeux[4] = {0};
the result
1606416356
1
Program ended with exit code: 0
So I haven't used the second array but the result of the first printf changes and I am a bit confused with what is going on.
I hope you can help me !
Please initialize the sum you return in function
int resultat = 0;
otherwise resultat will take garbage value as initial value