Question

In: Computer Science

In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a...

In C programming, Thank you

Declare an array that can contain 5 integer numbers.

Use a for loop to ask the user for numbers and fill up the array using those numbers.

Write a program to determine the 2 largest integers in the array, and print those numbers.

Hint: You can declare integer variables “largest” and “secondLargest” to keep track as you make comparisons using the if...elseif statement.

The following is an example of how your program should look when you execute it:

Enter a number to store in the array: 10
Enter a number to store in the array: 91
Enter a number to store in the array: 145
Enter a number to store in the array: 94
Enter a number to store in the array: 97

The largest number is 145
The second largest number is 97

Solutions

Expert Solution

#include <stdio.h>

int main() {
  int values[5];

  // taking input and storing it in an array
  for(int i = 0; i < 5; i++)
  {
         printf("Enter a number to store in the array: ");
     scanf("%d", &values[i]);
  }

  int max1=values[0];
  int max2=values[0];

  // printing elements of an array
  for(int i = 1; i < 5; i++)
  {
         if(values[i]>=max1)
         {
                max2=max1;
                max1=values[i];
         }
         else if(values[i]>=max2)
     {
         max2=values[i];
     }
  }
  printf("the largest number is : %d \n", max1);
  printf("the second largest number is : %d \n", max2);
  return 0;
}

Explanation of Code

1. First we have declared an array named 'values[5]' which can accomodate 5 integer type values.

2. Then we have used a for loop to enter values in the array.

  for(int i = 0; i < 5; i++)
{
   printf("Enter a number to store in the array: ");
scanf("%d", &values[i]);
}

3. Then we have made 2 integer type variables 'max1' to store largest value and 'max2' to store second largest value. max1 and max2 are initialized to 0th element of 'values[]'.

int max1=values[0];
int max2=values[0];

4. Then we have made another for loop. Inside the for loop we are checking if the current index value is greater than max1 or not. If current index value is greater than max1 then we are storing the value of max1 in max2 because this value becomes the second largest value now (first largest value is the current index value). Then we are storing the current index value in max1.

if(values[i]>=max1)
   {
      max2=max1;
      max1=values[i];
   }

5. Then we have made another else if condition because it is possible that the current index value is not greater than max1 but it might be greater than max2. If current index value is greater than max2 then we store the current index value in max2.

else if(values[i]>=max2)
{
max2=values[i];
}

6. At last we have printed max1 and max2.

printf("the largest number is : %d \n", max1);
printf("the second largest number is : %d \n", max2);


Related Solutions

In C++ Declare an integer array of size 20 and assign the array with 20 randomly...
In C++ Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {      ...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
declare an integer array of n numbers //enter a value for n before the declaration enter...
declare an integer array of n numbers //enter a value for n before the declaration enter values for the declared array, pass the array to the method that has the name "countNumbers" let the function countNumber count how many elements of the array in the interval [70 90] means between 70 and 90 inclusive, returns this count to the main, and prints it using format printing. --------------- Please Solve As soon as Solve quickly I get you thumbs up directly...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Explain the difference between array and structure based on their usage in C++ programming. Declare a...
Explain the difference between array and structure based on their usage in C++ programming. Declare a structure called studentScore which contains name of student, registration number of students, course code and marks. Declare structure variable named studentCS680 based on the structure in (b) to store fifty (50) students’ data. Write a program that prompts a user to enter data for 50 students in a structure variable declared in (b) and calculate the average mark.
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each...
(C programming) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it’s not a duplicate of a number already read. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. Your solution must include a function called isUnique() that returns 1 (true) if the number input is...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {             //...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT