Monday, 16 January 2017

Finding the position of an element in an array using arrays in C programming

Finding the position of an element in an array using arrays in C programming

The program is an example to search the components in the array. It may be characters or number. For this example, we are using numbers. If you give any number to the program. If is is in the array the computer prints the index of that number where it is found in the array. If it is not found in the array simply it'll print a message telling that the number is not found in the array.

If you want to see this code with a perfect indentation, copy the code into "sublime text editor" and make sure that the type of code is set to 'c' at the bottom right of the window. After pasting the code press the command "ctrl+shift+P", you get a search box. Type "indentation" you get an option like this below the search box "Indentation: Reindent lines". Clink on that to get the code with indentation. 

The program is as follows:

#include <stdio.h>
#define MAX_SIZE 1000
main()
{
int i,posi=0,n,d,arr[MAX_SIZE];
printf("enter the size of the array:\n");
scanf("%d",&n);
printf("enter the elements into the array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the number which is to be find within the array\n");
scanf("%d",&d);
for(i=0;i<n;i++)
if(d==arr[i])
posi=i+1;
if(posi!=0)
printf("the position of the number you entered in the array is: %d\n",posi );
if(posi==0)
printf("the number you have entered is not in the array\n");
return 0;
}

The output is as follows:







#include <stdio.h>
The '#include' is a preprocessor directive which includes the source files inside the code. The 'stdio.h' is a standard input and output header file with several functions written in it. The functions like 'printf()''gets()' and 'scanf()' are work when we include 'stdio.h' header file. 

#define MAX_SIZE 1000

'#define' is also a preprocessor directive which declares the universal variables. In the above line "MAX_SIZE" is the universal variable. We assigned the universal variable with 1000. Throughout the program, it'll remain same. No changes will occur to it.



int i,posi=0,n,d,arr[MAX_SIZE];

We declared the integers 'i', 'posi', 'n', 'd' and an array named as 'arr[MAX_SIZE]'. The size of the array is 1000. A maximum of thousand values you can enter into that array or you can put a limit on the input and you can use up to that limit.


printf("enter the size of the array:\n");
scanf("%d",&n);

The function 'ptintf()' prints the message "enter the size of the array". It is a call to enter the size of the array. When the user enters a value and presses the enter button. The computer stores the integer value into the variable 'n'.

printf("enter the elements into the array:\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);

Again the function 'printf()' prints the message "enter the elements into the array". The user should enter the number of elements which is equal to the size of the array. After entering all the elements using the loop the computer assigns all the values into the compartments of the array.

printf("enter the number which is to be find within the array\n");
scanf("%d",&d);

Our task is to find the position of the element in the array or to find whether the number is in the array or not. So, a 'printf()' function prints the message "enter the number which is to be found in the array". After entering the number it stores the integer in the variable 'd'.

for(i=0;i<n;i++)
if(d==arr[i])
posi=i+1;

This for loop is to find the number which is to be searched. The 'if condition' checks whether the integer value in the variable 'd' is matches with the element in the compartment of the array 'arr[]'. If the element is matched the body of 'if condition' is executed. The variable 'posi' is stored in an integer which is incremented with '1' to the value of 'i'.

if(posi!=0)
printf("the position of the number you entered in the array is: %d\n",posi );
if(posi==0)
printf("the number you have entered is not in the array\n");

This two conditions will check the value of 'posi' to print the output. If the element is found the message "the position of the number you entered in the array is: 'integer value of the variable posi' ". Else the message "the number you have entered is not in the array". Hence the problem is solved, another program gets into the bucket.



C is a general-purpose programming language. It has been closely associated with the UNIX system where is was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a “system programming language” because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7.
BCPL and B are “typeless” languages. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.
C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible cases (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).
Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically “automatic,” or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist I separate source files that are compiled separately. Variables may be internal to a function, external but know only within a single source file, or visible to the entire program.
A preprocessing step performs macro substitution on program text, inclusion of other source files, conditional compilation.
C  is a relatively “low level” language. This characterization is not pejorative; it simply means that C deals with the same sort of object that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.
C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is n heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.

Vertical histogram of frequencies of words in a given string using an example in C programming

No comments :

Post a comment