What is wrong in the following program? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

What is wrong in the following program?

https://code.sololearn.com/cm14swVMJ8yi/?ref=app pls help(^^)

10th Nov 2018, 10:16 AM
Abhay
Abhay - avatar
17 Answers
+ 9
Sorry, I didn't mean to confuse you, but let's have an example, user input 5 for array elements, then the 995 elements out of 1000 are not used, it's not efficient, but if you size the array as necessary (just 5 elements) then there is no memory wasted. You can declare the array after reading <n> (the number of array elements) from user, then, as I had written in my fix, you can do: long int a[n]; This will create the array with <n> elements, more efficient.
10th Nov 2018, 6:40 PM
Ipang
+ 7
You are always welcome my friend, and no worries, you didn't waste my time, and I'm happy you understand about it now ^_^
10th Nov 2018, 6:53 PM
Ipang
+ 7
I made some test and it seems working now. I just suggest you to add some "\n" in the printf in order to separate the lines and make the text more comprensible. Good work done.
11th Nov 2018, 11:52 AM
Daniele Gaito
Daniele Gaito - avatar
+ 6
Here's your fixed code, compare with original version then you'll see the problems. #include <stdio.h> long int searchnum(long int[],long int,long int); int main() { long int i, n, search, position; printf("Enter number of elements in array:"); scanf("%ld", &n); long int a[n]; printf("%ld\nNow enter %ld numbers.\n", n, n); for(i = 0; i < n; i++) { scanf("%ld", &a[i]); printf("%ld ", a[i]); } printf("\nInput a number to search:"); scanf("%ld", &search); printf("%ld\n", search); position = searchnum(a, n, search); if (position == -1) printf("%ld Isn't present in array\n", search); else printf("%ld Is present at location %ld\n", search, position); return 0; } long int searchnum(long int b[], long int s, long int find) { for(long int i = 0; i < s; i++) if(b[i] == find) return i; return -1; }
10th Nov 2018, 4:16 PM
Ipang
+ 6
You're welcome Abhay, also here use the number of array elements input from user, rather than static size of 10 elements, it was the reason we ask them to enter number of array elements : )
10th Nov 2018, 6:13 PM
Ipang
+ 5
Why ask number of array elements if you have statically dimension the array as 1000 elements?
10th Nov 2018, 6:20 PM
Ipang
+ 5
Declare your variable before any executable statement(as a first line after starting of function), because variables in c can't be declared after any executable statement.
14th Nov 2018, 6:32 PM
#DARK_PROGRAMMER_✔
#DARK_PROGRAMMER_✔ - avatar
+ 4
and if you want to declare a dynamic size array then declare it with a star '*' example 'long int *a;' instead of 'long int a[n];'
14th Nov 2018, 6:35 PM
#DARK_PROGRAMMER_✔
#DARK_PROGRAMMER_✔ - avatar
+ 3
I removed the first brace after long int variables in main()and the brace before return -1 ,removed semicolon after for() in main and also in calling function I was using a array when i took b ,now it works well
10th Nov 2018, 5:10 PM
Abhay
Abhay - avatar
+ 2
so thks
10th Nov 2018, 5:10 PM
Abhay
Abhay - avatar
+ 2
didn't I asked?
10th Nov 2018, 6:16 PM
Abhay
Abhay - avatar
+ 2
I made it 1000 now ,and inputted array size -2
10th Nov 2018, 6:17 PM
Abhay
Abhay - avatar
+ 2
I can't leave it empty
10th Nov 2018, 6:31 PM
Abhay
Abhay - avatar
+ 2
you are confusing me ,it's just what I have read ,when you input array size you give some random size array but you should input size less than the taken one
10th Nov 2018, 6:33 PM
Abhay
Abhay - avatar
+ 2
right that's better it will save a lot of memory now I see why you did that in your code ,before I didn't understood the purpose and ignored it just ,ty again Ipang,sorry if I wasted your time !!(^^)
10th Nov 2018, 6:49 PM
Abhay
Abhay - avatar
0
still there are many errors!
10th Nov 2018, 11:54 AM
Abhay
Abhay - avatar
0
2 errors still left
10th Nov 2018, 12:10 PM
Abhay
Abhay - avatar