Question

(C programing, Not C++) Write a program in C that asks the user to input 10...

(C programing, Not C++) Write a program in C that asks the user to input 10 numbers.

Store these numbers in an array. Then ask the user to input a single number.

Your program should execute a linear search on the array, trying to find that number.

If the number exists in the array, have the program print out which position/element the

number was found at.

If the target number is not in the array, have the program print out a statement to that

effect.

Example:

./a.out

Please enter 10 numbers.

1 2 3 4 5 6 7 8 9 10

Now enter a number to search for.

5

The number 5 was found at element number 4.

./a.out

Please enter 10 numbers.

1 2 3 4 5 6 7 8 9 10

Now enter a number to search for.

11

The number 11 was not found in the array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main()
{
   int n = 10, i, k, res;
   int a[10];
   printf("Please enter 10 numbers.\n");

   for(i = 0;i<n;i++){
      scanf("%d",&a[i]);
   }
   
   printf("Now enter a number to search for.\n");
   scanf("%d",&k);
   
   res = -1;
   for(i = 0;i<10;i++){
      if(a[i] == k){
         res = i;
      }
   }
   
   if(res != -1){
      printf("The number %d was found at element number %d.\n", k, res);
   }
   else{
      printf("The number %d was not found in the array.\n", k);
   }
   
   return 0;
}
Add a comment
Know the answer?
Add Answer to:
(C programing, Not C++) Write a program in C that asks the user to input 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • In Small Basic Write a program that asks the user how many numbers s/he wishes to...

    In Small Basic Write a program that asks the user how many numbers s/he wishes to enter into an array and then enters a loop and proceeds to get those numbers from the user and enter them into the array. Once the array has been filled with the numbers, your program then asks the user to make a choice indicating what s/he wishes to do with the numbers in the array. The choices are: TextWindow.WriteLine("Enter 1 to compute and display...

  • In C++: Write a program that asks the user for an array of X numbers. Your...

    In C++: Write a program that asks the user for an array of X numbers. Your program moves the largest number all the way to the right, and then shows it to the user. Hint: Use a loop to swap X times, but only swap if the number on the left is bigger than the number on the right. Sample Run: How many numbers would you like to enter? 6 Please enter number 1: 10 Please enter number 2: 8...

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • 2) Write a program in C/C++ that asks the user to enter a number then the...

    2) Write a program in C/C++ that asks the user to enter a number then the user's input value is passed to a function which increments the number by 10 and returns the result of the incrementation. Write the program to accomplish the increment using three (3) different techniques. To test each technique separately, the user should enter a different number for each of the three (3) techniques. Make sure that you compile and execute your program. Finally, provide screenshots...

  • Write a C program which asks the user to enter a name of a person and...

    Write a C program which asks the user to enter a name of a person and the program finds the number of that person. The number is displayed. Otherwise output "Not found". Your program have to check only given names. The program declares and initializes a two-dimensional array of characters that holds pairs of names and numbers. The array can hold 10 strings, each capable of holding up to 79 characters. Array elements are following: "Adam", "585-5622", "Mark", "505-8446", "Austin",...

  • C programming

    Write a C program to take N numbers of integer in an array. Then take a number M from user and search it in the array. If number M is found in the array, then print “Number ta paisi!!”, and if not found then print “Pailam na vi number ta!!”.Sample Input:                                         Sample Output:Enter value of N: 5Enter numbers in array: 7...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • please only use java import j option pane. use modular programing please write a program with...

    please only use java import j option pane. use modular programing please write a program with a two dimensional array use int for array whole numbers are going to be use. the purpose is to fill the array with the item number and price of up to 100 products (up to 100 item numbers and up to 100 prices). then print out the array. Also verify that the user is entering a number greater then 0 for each input (item...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT