Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have written the following program:
#include <stdio.h>
void printValue();
int main (){
int n = 100;
int i;
for (i=0; i<n; i+=1)
printValue();
}
void printValue(){
static unsigned int y = 0;
printf("y = %d", y);
y+=1;
}
How can I rewrite the algorithm to make it recursive?
#include <stdio.h>
void printValue(void);
void times(int n, void (*func)(void)){
if(n>0){
func();
times(--n, func);
}
}
int main (void){
int n = 100;
times(n, printValue);
return 0;
}
void printValue(void){
static unsigned int y = 0;
printf("y = %d\n", y);
y+=1;
}
#include <stdio.h>
void printValue(int);
void repeat_upto(int init_value, int end_value, int incremental,
void (*func)(int)){
if(incremental < 0 ? init_value >= end_value : init_value <= end_value){
func(init_value);
repeat_upto(init_value + incremental, end_value, incremental, func);
}
}
int main (void){
repeat_upto(0, 100-1, +1, printValue);
return 0;
}
void printValue(int v){
printf("%d\n", v);
}
#include <stdio.h>
void printValue(int v, int end_value){
if(v < end_value){
printf("%d\n", v);
printValue(v+1, end_value);
}
}
int main (void){
printValue(0, 100);
return 0;
}
This is almost the same as BLUEPIXY's answer since I believe it's the straight forward solution, but since you are confused by the function pointer, I removed that.
#include <stdio.h>
#include <stdlib.h>
void
printValue()
{
static unsigned int y;
printf("%d\n", y);
y += 1;
}
void
recursiveFunction(int counter)
{
printValue();
if (--counter == 0)
return;
recursiveFunction(counter);
}
int
main()
{
recursiveFunction(100);
return 0;
}
Or may be you mean this
#include <stdio.h>
#include <stdlib.h>
void
printValue(int y)
{
if (++y > 100)
return;
printf("%d\n", y);
printValue(y);
}
int
main()
{
printValue(0);
return 0;
}
Instead of
void printValue()
{
static unsigned int y = 0;
printf("y = %d", y);
y+1;
}
I turn it into:
void printValue(int y)
{
y++;
printf("y = %d\n", y);
printValue(y);
}
Compiled -> His function
Compiled -> Recursive function
See Same output, I just did what OP wanted.
Personal I would do this without recursive function avoid infinite loop:
for (i=0; i<n; i++)
{
printValue(y);
}
void printValue(int y){
printf("y = %d\n", y);
}
Related
The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.
This question already has answers here:
Why is it safer to use sizeof(*pointer) in malloc
(3 answers)
What happens if I use malloc with the wrong size?
(4 answers)
Closed 2 years ago.
Here is my code with the print statement followed by its output.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
int *y = malloc(size);
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
return 0;
}
OUTPUT:
1 2 3 4 5 6 7
12 1 2 3 4 5 6 7
(The second matrix is what I want the code to output)
Here is my code without the print statement followed by its output.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
int *y = malloc(size);
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
// printf(" %d",*(y+i)); <-- ***THIS THING RIGHT HERE*** commented out
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
return 0;
}
OUTPUT:
12 1 2 3 4 5 83 0
(This is not what I want...)
Can somebody please explain where this code is pulling 83 and 0 for the last two elements in the array just because I chose to include or exclude a random print statement?
Any help would be greatly appreciated because I can't understand how C is pulling numbers out of thin air like this.
Your malloc size is not correct and causes undefined behavior.
The actual memory size is the length of array * size of array type.
The printf in main is actually printing something random in the memory.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int *myF(int a[],int s, int n);
int *myFunc(int x[],int size,int nnum) {
int i;
size += 1;
//int *y = malloc(size * sizeof(int) );
int *y = malloc(size );
for(i=0;i<size;i++) {
if(i==0)
*y = nnum;
else {
*(y+i) = x[i-1];
printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
}
}
return y;
}
int main (int argc, char *argv[]) {
int i;
int x[7] = {1,2,3,4,5,6,7};
int *P = myFunc(x,7,12);
for(i=0;i<8;i++) {
if(i==0) printf("\n");
printf(" %d",*(P+i));
}
free(P);
return 0;
}
If you're interesting in why printf changed the behavior, this post has in-depth explanation.
Also don't forget to free P in the end.
So I'm getting this message when I try to load my array using pointers.
I don't know why this keep appearing since the last program had no problem
#include<stdio.h>
#define T 10
void FLoad(int *);
void main () {
int a[T];
void FLoad(a);
}
void FLoad(int *a) {
int x;
for (x = 0; x < T; x++)
scanf("%d", a+x);
}
And here is a little program that works perfectly
#include <stdio.h>
void FImp(int *, int );
main () {
int a[] = {-10,-5,3,4}, tam;
tam = sizeof(a) / sizeof(int);
FImp(a, tam);
}
void FImp(int *a, int t) {
int x;
for (x = 0; x < t; x++)
printf("%d ",*(a + x));
putchar('\n');
}
You are using incorrect syntax when calling your function
void main()
{
int a[T];
void FLoad(a);
}
should be
void main()
{
int a[T];
FLoad(a);
}
or even better
int main(void)
{
int a[T];
FLoad(a);
}
You don't specify the function return value when you call it.
void FLoad(a);
This won't call the function. The compiler will consider this as a function declaration. So call the function without void it will work fine.
This question already has answers here:
conflicting types error when compiling c program using gcc
(3 answers)
Closed 7 years ago.
So I'm making a function that makes a table, that puts the x and y values through an equation. this is what I have so far.
#include <stdio.h>
#include <math.h>
int main(){
int x, y;
float num;
printf("%3c", '+');
for (x=5; x <= 100;x=x+5){
printf("%8i",x);
}
printf("\n");
for (y = 5; y<= 100;y=y+5){
printf("%3d ",y);
for (x=5;x<=100;x=x+5){
num = theMath(x, y);
printf("%7f", num);
printf(" ");
}
printf("\n");
}
return 0;
}
float theMath(int x, int y){
float sum;
sum = ((x*x*x*x)/(y*y)) + sqrt(y);
return sum;
}
It's telling me "Error: Conflicting types for 'theMath'", and I can't figure out why. Compiling with gcc.
Add a prototype definition(function declaration) of theMath before its function call. That should be the reason for the error.
#include <stdio.h>
#include <math.h>
//add declaration of the function here
float theMath(int x, int y);
int main(){
int x, y;
float num;
printf("%3c", '+');
for (x=5; x <= 100;x=x+5){
printf("%8i",x);
}
printf("\n");
for (y = 5; y<= 100;y=y+5){
printf("%3d ",y);
for (x=5;x<=100;x=x+5){
num = theMath(x, y);
printf("%7f", num);
printf(" ");
}
printf("\n");
}
return 0;
}
float theMath(int x, int y){
float sum;
sum = ((x*x*x*x)/(y*y)) + sqrt(y);
return sum;
}
Your function theMath is used before being defined, and it is not declared before usage. You just have to declare the function at the beginning of your C file:
float theMath(int x, int y);
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;
}