3). In a math class you learn about the different operations that can be done to a series. Imagine the teacher have asked you to take 3 numbers. Arrange them in an increasing order. If the difference between the third number and the second number is less than the difference between second and first, display the square of each number in the series, or if the difference between the third number and the second number is greater than the difference between second and first, display the cube of each number in the series, else display the series. Write a C program to implement the above logic using suitable if statements and appropriate mathematical function. Sample Inpu and Оuгршt: Input 3 numbers::1O 2 30 Sort 2 1o 30 Cube Series:8 1000 27000 Input 3 numbers::12 10 11 Sort 10 11 12 Series is 10 11 12 %3D

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 using c_compiler 

3). In a math class you learn about the different operations that can be done to a series. Imagine the
teacher have asked you to take 3 numbers. Arrange them in an increasing order. If the difference
between the third number and the second number is less than the difference between second and
first, display the square of each number in the series, or if the difference between the third number
and the second number is greater than the difference between second and first, display the cube of
each number in the series, else display the series. Write a C program to implement the above logic
using suitable if statements and appropriate mathematical function.
Sample Inpu and Оuгршt:
Input 3 numbers::1O 2 30
Sort 2 1o 30
Cube Series:8 1000 27000
Input 3 numbers::12 10 11
Sort
10
11
12
Series
is
10
11
12
%3D
Transcribed Image Text:3). In a math class you learn about the different operations that can be done to a series. Imagine the teacher have asked you to take 3 numbers. Arrange them in an increasing order. If the difference between the third number and the second number is less than the difference between second and first, display the square of each number in the series, or if the difference between the third number and the second number is greater than the difference between second and first, display the cube of each number in the series, else display the series. Write a C program to implement the above logic using suitable if statements and appropriate mathematical function. Sample Inpu and Оuгршt: Input 3 numbers::1O 2 30 Sort 2 1o 30 Cube Series:8 1000 27000 Input 3 numbers::12 10 11 Sort 10 11 12 Series is 10 11 12 %3D
Expert Solution
Step 1

Below is C program with explanation: -

Explanation: -

For the given problem array is used to sort the 3 numbers and then print the series.

  • Including the Header file.
  • Defining the method Sort(int x_1[3]) to sort the elements.
  • Using the nested for -loop to iterate over the array.
  • Inside the nested loop, the if-statement is used to compare the adjacent elements.
  • This method will return the sorted array.
  • Defining the main method.
  • Declaring the array arr[3] to store the three elements entered by the user.
  • Prompts the user to enter the three elements. Using the for-loop to fill the array.
  • Calling the method Sort and passing the inputted array.
  • Displaying the sorted elements (sorted array by using the for-loop).
  • The if-elseif-else statement is used to check for the different conditions and print the series (square, cube, or series) accordingly.
Step 2

C program: -  

//Including the Header file

#include <stdio.h>

//Defining the method to sort the elements

int Sort(int x_1[3])

{

//Declaring the variables 

int i,j,t;

//Nested loop to iterate over the array 

for(i = 0; i<3-1; i++)

{

  for (j =0; j<3-i-1; j++)

  {

    //to compare the adjacent elements

    if (x_1[j] > x_1[j+1])

    {

      //swapping the values

      t = x_1[j];

      x_1[j] = x_1[j+1];

      x_1[j+1] = t;

    } 

  }

}

//return the sorted array

return x_1[i];

}

//Defining the main method

int main()

{

//Declaring the array with size 3 

int ar[3];

//Prompts the user to enter the elements of an array

printf("Input 3 numbers::");

//Loop to fll the array 

for(int i =0; i<3;i++)

{

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

}

//Calling the sort method and passing the inputted array

Sort(ar);

printf("Sort ");

for(int i =0; i<3; i++)

{

  //Displaying the sorted array

  printf("%d ",ar[i]);

}

//Printing the squares of the series   

if(ar[2]-ar[1] < ar[1]-ar[0])

{

  printf("\nSquare Series:"); 

  for(int i=0;i<3;i++)

  {

    printf("%d ",ar[i]*ar[i]);

  }

}

//Printing cubes of series

else if(ar[2]-ar[1] > ar[1]-ar[0])

{

  printf("\nCube Series:");

  for(int i=0;i<3;i++)

  {

    printf("%d ",ar[i]*ar[i]*ar[i]);

  }

}

//else printing the series

else

{

  printf("\nSeries is :");

  for(int i=0;i<3;i++)

  {

    printf("%d ",ar[i]);

  }

}

}

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education