can not run the simple function program - c

#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it

Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)

#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}

Related

Why does my code work without "void" in my main function?

I have to do a short assignment for my introductory C class, where I have to ask for a number "N" from the user and calculate it's factorial. The requirements were for me to create a function with the prototype long int factorial(int N). I managed to do it, but I'm confused as to why my code is working with a specific change I made. Here is my code:
#include <stdio.h>
long int factorial(int);
int main(void)
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
My code at this point didn't work, and would just return the result of N+1 (if I input 5 for example, it would output 6). I was tweaking random things at this point to see what was the problem, and the removal of "void" in my main function fixed it. The problem is, I don't understand why.
#include <stdio.h>
long int factorial(int);
int main()
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
Could anyone explain why the removal of void in my code fixed this?

Taking Output of function and putting it into another function in C

So, I'm trying to put the output of one function and put it to another function
Here are the functions that I'm trying to get output and inputs, you can just ignore inside of a second function I just simply put printf to check if the variables are correct.
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
And here is the code:
#include <stdio.h>
int main(){
int row, column;
guess(row, column);
check(row, column);
}
int guess(int a, int b){
printf("\nEnter you guess: ");
scanf("%d,%d", &a, &b);
return a, b;
}
int check(int a, int b){
printf("%d %d ",a,b);
}
I tried to put it simply to understand how to do it more clearly.
When I run the code and put coordinates for example: 4,5 and it only prints out 0 1
Also, Is it possible to do it with arrays?
1) You need to use a prototype or declare your functions forward.
2) You can not return 2 variables form a function in C, but you can pass an array and read/write his values:
#include <stdio.h>
void guess(int arr[])
{
printf("\nEnter you guess: ");
scanf("%d,%d", &arr[0], &arr[1]);
}
void check(int arr[])
{
printf("%d %d ",arr[0], arr[1]);
}
int main(void)
{
int arr[2];
guess(arr);
check(arr);
return 0;
}
or you can pass a reference
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
guess(&a, &b);
The input numbers you read in guess functions are actually only read into the local variables a and b. You'd need to pass pointers to be able to read into the vars in main.
Also there's no way to return multiple values from a function in C.
#include <stdio.h>
void guess(int *a, int *b)
{
printf("\nEnter you guess: ");
scanf("%d,%d", a, b);
}
int check(int a, int b)
{
printf("%d %d " ,a, b);
}
int main()
{
int row = 0, column = 0;
guess(&row, &column);
check(row, column);
}
You should also check the return value of scanf for failures.

DFS Adjacency matrix

I'm trying to make a directed DFS traversal path from an adjacency matrix. Basically print out the path in nodes, but the output is always bad for some reason. Even though the code looks sound, it never actually follows the path.
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void DFS(int i, int graph[][MAX], int n, int visited[MAX] );
void visit_all(int graph[][MAX], int n);
void read_matrix(int graph [][MAX], int n);
int main() {
int graph[MAX][MAX];
int n;
printf("Input matrix dimension: ");
scanf("%d", &n);
if (n>MAX) {
fprintf(stderr, "Too large\n");
exit(1);
}
read_matrix(graph, n);
visit_all(graph, n);
return 0;
}
void DFS(int v, int graph[][MAX], int n, int visited[MAX]) {
int w;
printf("%d ", v);
visited[v] = 1;
for(w=0;w<n;w++)
if (visited[w] == 0 && graph[v][w] == 1)
DFS(w, graph, n, visited);
}
void visit_all(int graph[][MAX], int n) {
int v, visited[MAX];
for(v=0;v<n;v++)
visited[v] = 0;
for(v=0;v<n;v++)
if (visited[v] == 0)
DFS(v, graph, n, visited);
printf("\n");
}
void read_matrix(int graph [][MAX], int n) {
int i, j;
for(i=0;i<n;i++)
for (j=0;j<n;j++)
graph[i][j] = 0;
printf("\Input elements in format [ij], CTRL+D to end\n");
while(scanf("%d%d", &i, &j) != EOF) {
graph[i][j] = 1;
}
}
Input elements in format [ij]
Scanf of 57 using %d%d will not give you 5,7 – stark

Why is the following code showing run-time error(SIGSEGV)?

This is my solution to the Prime Generator problem on SPOJ(Sphere Online Judge)(http://www.spoj.com/problems/PRIME1/). I have allocated memory using malloc(), but it is still showing a run-time error(SIGSEGV).
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
scanf("%d", &n);
while(n--){
int a,b;
scanf("%d %d", &a, &b);
int *z =malloc(sizeof(int)*(b-1));
// Filling the array
int i;
for(i=0;i<b-1;i++){
z[i]=i+2;
}
int k,p;
for(k=0;k<=b-2;k++){
if(z[k]){
if(z[k]>=a){
printf("%d\n", z[k]);
}
for(p=k+z[k];p<=b-2;p+=z[k]){
z[p]=0;
}
}
}
free(z);
}
return 0;
}

reversing elements of an integer array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

Resources