I'm trying to run a very simple code using a void function, but no matter what I try or some error occurs, or the program doesn't print what it was supposed to. The code is
#include <stdio.h>
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
I am trying to use void in other more complex program so I am using this very simple to discover how to make it.
You need to give a prototype (or definition) of a function before you use it in a program.
Definition
Shift the function add before main function:
#include <stdio.h>
void add(int i, int j)
{
printf("%d + %d = %d", i, j , (i+j));
}
int main()
{
int i,j;
i = 1;
j=2;
add( i, j);
return 0;
}
Prototype
#include <stdio.h>
void add(int,int);
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
Change the order so that add is read first
#include <stdio.h>
void add(int i, int j)
{
printf("%d + %d = %d", i, j, (i+j));
}
int main()
{
int i,j;
i = 1;
j = 2;
add(i, j);
return 0;
}
Related
I've tried to solve "The 3n+1" problem.
When I debug my code it stuck at line 12, calculation function.
"According to Collatz conjecture, j should converge to 1."
Main file
#include "input_output.h"
#include <stdlib.h>
int main() {
int i=0, j=0;`
int *num;
int maxCycle;
int length;
input(&i, &j);
length = j - i + 1;
num = (int*)malloc(sizeof(int)*(j - i+1));
here is the problem code
while (i <= j) {
calculate(j, num);//<- it stuck at here when i dubug it.
j--;
num++;
}
maxCycle = findMax(length, num);
output(maxCycle);
return 0;
}
source file
#include <stdio.h>
#include "input_output.h"
#pragma warning (disable:4996)
void input(int *i, int *j) {
scanf("%d %d", i,j);
}
void calculate(int j, int* num) {
while (j > 1) {
if (j % 2 == 0) {
j = j / 2;
*num++;
}
if (j % 2 == 1) {
j = j * 3 + 1;
*num++;
}
}
}
int findMax(int length, int * num){
int max = 0;
int idx = 0;
while (idx < length) {
if (*num > max) max = *num;
idx++;
num++;
}
return max;
}
void output(int maxout) {
printf("%d", maxout);
}
Header
#ifndef __input_output_H__
#define __input_output_H__
void input(int *i, int *j);
void calculate(int j,int *num);
int findMax(int length, int* num);
void output(int maxout);
#endif __input_output_H__
I think header seems no problem and also main file.
is there any problem with my source file?
I wonder why debugger stuck at there...
Your loop never ends: you reach j == 1, yet you continue to apply 3n + 1, which makes you go back to 4, and therefore you are in a loop forever:
1 -> 4 -> 2 -> 1 -> ...
By the way, this:
*num++;
is not doing what you think it is doing. You are incrementing the pointer, and then accessing the value (which is not used). So it is as if you had written:
num++;
You should have written (*num)++.
The problem is at j = j * 3 + 1;, 'j' keeps on increasing if 'j' is greater than 1 and odd. So it hangs at calculate(int j,int *num) as the while loop inside it runs infinitely (j value will reset after a while).
Edit:
I have accumulated all the corrections and have added the code:
main.c :
#include "input_output.h"
#include <stdlib.h>
int main()
{
int i=0, j=0;
int *num,*ori; //New pointer required to remember the start position of num
int maxCycle;
int length;
input(&i, &j);
length = j - i + 1;
num = (int*)calloc((size_t)(j-i+1),sizeof(int));
ori=num;
while (i <= j)
{
calculate(j, num);
j--;
num++;
}
num=ori;
maxCycle = findMax(length, num);
num=ori;
output(maxCycle);
return 0;
}
input_output.h :
#ifndef INPUT_OUTPUT_H
#define INPUT_OUTPUT_H
void input(int *i, int *j);
void calculate(int j,int *num);
int findMax(int length, int* num);
void output(int maxout);
#endif
input_output.c :
#include <stdio.h>
#include "input_output.h"
void input(int *i, int *j) {
printf("Enter the i & j:\n");
scanf("%d%d",i,j);
printf("Values entered:\ni: %d\nj: %d",*i,*j);
}
void calculate(int j, int* num) {
while (j > 1) {
if (j==1)
break;
if (j % 2 == 0) {
j = j / 2;
(*num)++;
}
else{
j = j * 3 + 1;
(*num)++;
}
}
printf("\nLength Value: %d\n",*num);
}
int findMax(int length, int * num){
int max = 0;
int idx = 0;
printf("\nLength Values:\n");
while (idx < length) {
printf("%d ",*num);
if (*num > max)
max = *num;
idx++;
num++;
}
return max;
}
void output(int maxout) {
printf("\nResult: %d", maxout);
}
Compile in linux using: gcc input_output.c main.c -Wall -Wextra -pedantic -Wconversion -std=gnu11 -o collatz
For further clarification please comment.
This code is to print reverse of array using recursion
I am using recursion function called from main
output should be as 5,4,3,2,1
can someone help in debugging this
#include <stdio.h>
void recursion(int a[])
{
int i=0;
if(i<5)
return;
i++;
recursion(i);
printf("%d ",a[i]);
}
int main()
{
int arr[]={1,2,4,5};
recursion(arr);
}
A good C book is needed:
#include <stdio.h>
void recursion(int a[], int i, int size)
{
if(i < size -1)
recursion(a, i + 1, size);
printf("i = %d arr[%d] == %d \n",i, i, a[i]);
}
int main()
{
int arr[] = {1, 2, 4, 5};
recursion(arr, 0, sizeof(arr) / sizeof(arr[0]));
}
Your recursion function is not working properly.
Note whenever you are calling a function the parameters should be same. In your code parameter of your function is array, but when you are calling it from inside the function i.e recursion you are passing i.
So, what you can do is that
#include <stdio.h>
void recursion(int a[], int i)
{
int t = i-1;
if(t>=0)
{
printf("%d ",a[t]);
return recursion(a, t);
}
}
int main()
{
int arr[]={1,2,4,5};
recursion(arr,4);
}
Hopefully that helps
I feel like I've attempted every combination I know of to get this to work and can't figure it out. How can I scanf() into an int** passed as a pointer to a function? I tried searching but couldn't find this, if it's a duplicate please let me know and I'll delete. It begins to run and after entering a few values it segfaults.
Here's my code, I think it's messing up on the scanf() line of the setMatrix() function:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int ***mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", mat[i][j]); // problem here??
printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
}
}
return;
}
// print matrix
void printMatrix(int ***mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", (*mat)[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(&mat, r, c);
printMatrix(&mat, r, c);
}
There is no need to use triple pointer ***. Passing two-dimensional array will work as is. Here is the code:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int **mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]); // no problem here
printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
}
}
}
// print matrix
void printMatrix(int **mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(mat, r, c);
printMatrix(mat, r, c);
}
Should be:
scanf("%d", &(*mat)[i][j]);
You're passing a pointer to you matrix object, so you need to dereference it (with *) just as you do with printf. scanf then needs the address of the element to write into, so you need the &
I coded as below to print all the permutations of three number :1,2,3.
But the output is:
1,1,1
1,1,2
1,1,3
1,2,1
1,2,2
1,2,3
The code is as follows:
#include<stdio.h>
#include<conio.h>
void perm(int);
int a[10],l=2;
int main()
{
int k;
k=0;
perm(k);
getch();
return 0;
}
void perm(int k)
{
int i;
for(a[k]=1;a[k]<=3;a[k]++)
{
if(k==2)
{
for(i=0;i<3;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
else
{
k++;
perm(k);
}
}
}
Please give the correct code.
Why do you increment k? k should not change for a given call to perm().
Also it's a bit too bad to be stuck with 3 permutations, you can easily generalize this way:
#include<stdio.h>
#include<conio.h>
static void perm(int, int);
static void all_perm(int);
int a[10];
int main()
{
all_perm(3);
getch();
return 0;
}
void all_perm(int n)
{
perm(0, n);
}
void perm(int k, int n)
{
if (k == n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(a[k]=1; a[k] <= n; a[k]++)
{
perm(k + 1, n);
}
}
}
Edit: Well, what you name permutations are not permutations.
The logic that I have used is to some extent similar to yours.
I have included the entire code to make it clear.
#include <stdio.h>
void recn(int*,int,int);
void print_arr(int*,int);
void main()
{
int arr[3] = {1,2,3};
recn(arr,3,0);
}
void print_arr(int *arr, int n){
int i;
for(i = 0,printf("\n"); i < n; printf("%d",arr[i++]));
}
void recn(int *arr, int n, int l) {
int i, j, f, k, xx = 0;
static int tst[15], a[14]={0};
if (l == n) {
for (i = 0; i < n; i++) {
tst[i] = arr[a[i]];
}
print_arr(tst,n);
return;
}
for (i = 0; i < n; i++) {
f = 0;
for (j = 0; j < l; j++)
if (a[j] == i)
f = 1;
if (!f) {
a[l] = i;
recn(arr, n, l + 1);
}
}
}
I'm trying to understand why would the foo function trigger the linker error "undefined reference to foo" only if I place its definition last in the list, and if the declaration is anywhere else upper in the list, the error isn't there anymore.
If it helps, I'm using DevC++ 5.3.0.4 to create a C project with this file as main file.
All the other function work properly, and I'd like to understand why the position of the definition is so important, since this function doesn't use the other functions?
Initially, it did, but while trying to figure out why this error appeared I've eliminated what I thought to be possible causes, thus all other function calls within the foo function body.
I guess it might have something to do with the array parameter, but what is so special about this function and not about the others?
#include <stdio.h>
#include <stdlib.h>
void removeDuplicate(int position, int vector[], int n);
int belongsToSet(int x, int v[], int vectorDimension);
int isDuplicate(int position, int vector[], int n);
void displaySet(int v[], int dimension);
int foo(int v[]);
int main(int argc, char *argv[]) {
int m = -1, n = -1, i, j;
int a[20], b[20], reunion[40], intersection[40], abDifference[20],
baDifference[20];
int reunionIndex = 0, intersectionIndex = 0, abDifferenceIndex = 0,
baDifferenceIndex = 0;
i = foo(a);
getch();
return 0;
}
int belongsToSet(int x, int v[], int vectorDimension){
int i;
for (i = 0; i < vectorDimension; i++){
if (v[i] == x){
return 1;
}
}
return 0;
}
void removeDuplicate(int position, int vector[], int n){
int i;
for (i = position; i < n - 1; i++){
vector[i] = vector[i + 1];
}
}
int isDuplicate(int position, int vector[], int n){
int i;
for (i = 0; i < n; i++){
if ((vector[i] == vector[position]) && (i != position)){
return 1;
}
}
return 0;
}
void displaySet(int v[], int dimension){
printf("\n");
if (dimension == 0){
printf("\xed");
}else{
int i;
printf("{");
for (i = 0; i < dimension; i++){
printf("%d, ", v[i]);
}
printf("\b\b}");
}
int foo(int v[]){
return 1;
}
}
foo at the bottom, as it appears in the code presented, is INSIDE the definition of displaySet.