not able to print integers stored in array , just getting bunch of 0s | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

not able to print integers stored in array , just getting bunch of 0s

below is my code #include<stdio.h> #include<stdlib.h> int size_g; int cm=0; int final_array[10000]; //this is prototype int* data_collect(); int reduce(int a,int b, int c); //you are returning.....pointer to an object..so u need to add the return type as integer pointer-> int* int main() { int* p; p=data_collect(); for ( ; size_g > cm ; cm++) { if (cm>0) { reduce(p[cm],p[cm+1],final_array[cm-1]); } } int f = 0 ; while (f<size_g) { printf("%d",final_array[f]); f++; } return -1; } int* data_collect() { int k,i=0; printf("how many numbers you want to enter this time\n"); scanf("%d",&size_g); int* array_i=(int*)malloc(size_g*sizeof(int)); while(i<size_g) { printf("Enter the number : "); scanf("%d",&array_i[i]); ++i; } return array_i; } int reduce(int a , int b, int c) { c = a+b ; return c ; }

1st Nov 2017, 4:34 PM
Hridyansh Thakur
Hridyansh Thakur - avatar
2 Respostas
+ 5
As far as I can see, I think the problem is that you never assign final_array any value. I think you should either print p itself, or copy 'p' to final_array using a for loop, memmove or memcopy. And if you want reduce() to assign your values, you need to pass these values by reference, not by values, otherwise they will remain unchanged. So change the prototype to reduce(int,int,int&);
1st Nov 2017, 5:33 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
Debugging hints*: Add a function to trick SoloLearn into accepting input from you (never call it; this lets you keep your scanf()'s and then enter input like the rest of us): ============================== #include <iostream> void trickit(){ int foo; std::cin >> foo; } Wrap your reduce() in some debugging to see what's happening: ============================== if (cm>0) { // what are you actually sending to reduce()? std::cout << "\n: "<< p[cm] << " " << p[cm+1] << " " << final_array[cm-1] << std::endl; // does reduce() return anything? std::cout << "[reduce: " << reduce(p[cm],p[cm+1],final_array[cm-1]) << "]" << std::endl; } Also, returning something other than 0 from main() tells the parent process / the OS that your process failed: ============================== return -1; * EDIT: if trying in SoloLearn's c++ ... as @Gordie notes you appear to be using C / I do not want to confuse the reduce() signatures.
1st Nov 2017, 6:12 PM
Kirk Schafer
Kirk Schafer - avatar