+ 7
What is wrong in the following program?
https://code.sololearn.com/cm14swVMJ8yi/?ref=app pls help(^^)
17 Antworten
+ 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.
+ 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 ^_^
+ 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.
+ 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;
}
+ 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 : )
+ 5
Why ask number of array elements if you have statically dimension the array as 1000 elements?
+ 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.
+ 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];'
+ 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
+ 2
so thks
+ 2
didn't I asked?
+ 2
I made it 1000 now ,and inputted array size -2
+ 2
I can't leave it empty
+ 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
+ 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 !!(^^)
0
still there are many errors!
0
2 errors still left