[wpseo_breadcrumb]

return statement in c ??? why this function always returning 20?

Solutons:


You have a problem in your code. Change

scanf("%d",arr[i]);

To

scanf("%d",&arr[i]);

This is done because scanf expects an argument of type int* but you provide argument arr[i] which is of type int. Also add a check that ends the program if user inputs a number which is greater than 10 for the first scanf.

There can be two reasons.

Case 1 [Much likely for _always_]

Simple. Because your if(arr[i]==n) condition is not met, and i<m became false. It came out of for() loop and hence, return 20.

case 2 [Less likely for _always_]

By chance, the value of n is present at the 21st location [index 20] in the input array.

Apart from the coding aspect, did you understand what’s the logical purpose of this function? If not, begin with that. It searches for a specific value in an array of given length, and if no element of the array matches that value, it returns 20.

Now analyze your case, based on your input.


EDIT:

After seeing the complete code, as Mr. CoolGuy has pointed out, use

scanf("%d",&arr[i]);

Just for more reference, you can look at Chapter 7.19.6.2, paragraph 12 , %d format specifier, which goes like

… The corresponding argument shall be a pointer to signed integer.

In your code, arr[i] is of type int. What you need is a int *, i.e., &arr[i].

Related Solutions

What does __all__ mean in Python?

Linked to, but not explicitly mentioned here, is exactly when __all__ is used. It is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the following code in a foo.py...

Is the linux kernel ported to JavaScript yet?

Javascript is not a systems programming language, it is not appropriate for a kernel. Additionally, the kernel is a very large body of code, and "porting" it to another language is not something that can be done easily, and would likely take years. If the...

How to insert (file) data into a PostgreSQL bytea column?

as superuser: create or replace function bytea_import(p_path text, p_result out bytea) language plpgsql as $$ declare l_oid oid; begin select lo_import(p_path) into l_oid; select lo_get(l_oid) INTO p_result; perform lo_unlink(l_oid); end;$$; lo_get was...

What is the best color combination for on screen reading?

Legibility depends on high contrast between foreground and background, so black-and-white is the safest bet. See for example: Hall RH & Hanna H 2003. The Impact of Web Page Text-Background Color Combinations on Readability, Retention, Aesthetics, and...

MATCH FULL vs MATCH SIMPLE in foreign key constraints

Check the CREATE TABLE page of the manual: There are three match types: MATCH FULL, MATCH PARTIAL, and MATCH SIMPLE (which is the default). MATCH FULL will not allow one column of a multicolumn foreign key to be null unless all foreign key columns are null; if...

JavaScript set object key by variable

You need to make the object first, then use [] to set it. var key = "happyCount"; var obj = {}; obj[key] = someValueArray; myArray.push(obj); UPDATE 2021: Computed property names feature was introduced in ECMAScript 2015 (ES6) that allows you to dynamically...

What is the difference between const and readonly in C#?

Apart from the apparent difference of having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the constructor exits. After that it is frozen. const's are implicitly...

How should I index a UUID in Postgres?

Use PostgreSQL's built-in uuid data type, and create a regular b-tree index on it. There is no need to do anything special. This will result in an optimal index, and will also store the uuid field in as compact a form as is currently practical. (Hash indexes in...

Top level domain/domain suffix for private network?

Since the previous answers to this question were written, there have been a couple of RFCs that alter the guidance somewhat. RFC 6761 discusses special-use domain names without providing specific guidance for private networks. RFC 6762 still recommends not...

Getting last modification date of a PostgreSQL database table

There is no reliable, authorative record of the last modified time of a table. Using the relfilenode is wrong for a lot of reasons: Writes are initially recorded to the write-head log (WAL), then lazily to the heap (the table files). Once the record is in WAL,...

How do I make this sed script a “one liner”?

An ANSI C string -- with $'' -- can contain backslash escapes, like \n -- so you can have a newline in sed's arguments while still having the shell command invoking sed be only one line. sed -i $'/INTERPRETER_PYTHON_DISTRO_MAP/,/version_added/ {\n /default/a\\...

How to OCR a PDF file and get the text stored within the PDF?

ocrmypdf does a good job and can be used like this: ocrmypdf in.pdf out.pdf To install: pip install ocrmypdf or sudo apt install ocrmypdf # ubuntu sudo dnf -y install ocrmypdf # fedora After learning that Tesseract can now also produce searchable PDFs, I found...

If …Else If wont work [closed]

First of all, put the javascript code between a <script></script> tag because javascript code will not run in an html <div></div> tag. Then, instead of x == 0||9||2, use x == 0 || x == 9 || x == 2. Kindly indent your code for easier...

How to convert the object of character to string

Your object of characters is already almost an array. It has numeric indices, but is missing the .length property. If you add that it, it will be an "array like" object, which can then be passed to Array.from to get a proper array. Once you have a real array,...

How can I write the approximate value of PI?

Because your { and } is wrong. I think brackets will be as given below If the formula is PI = 4/1 - 4/3 + 4/5 - 4/7 + ... ( Leibniz's Series ) then you can formalate as given below #include <iostream> using namespace std; int main() { double n, i; //...