Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form

Write a program that inputs the names of students of a class and displays them in ascending and descending order.

Posted on October 16, 2021October 17, 2021 By myustaadg.com
Spread the love

Write a program that inputs the names of students of a class and display them in ascending and descending order.

So let’s Write Code:

Introduction to C++ Programming | Download and Install Dev C++ Compiler

Let’s build logic First:

Algorithm for code:

  • Step 1: Start
  • Step 2: Declare 2D array.
  • Step 3: Take inputs from user.
  • Step 4: Defining function for ascending order.
  • Step 5: Defining function for decending order.
  • Step 6: Calling the functions.
  • Step 7: Print elements.
  • Step 8: End program

Code:

Write a program that inputs the names of students of a class and displays them in ascending and descending order.

#include<iostream>
#include<string.h>
using namespace std;
main()
{
int i;
char names[10][20];
void sort_asc(char [10][20]);
void sort_des(char [10][20]);
for(i = 0; i<=9; i++)
{
cout<<"Enter value of student no. "<<i+1<<" ? ";
cin>>names[i];
}

sort_asc(names);
cout<<"\nStudent names in ascending order\n";
for(i = 0; i<=9; i++)
cout<<names[i]<<endl;

sort_des(names);
cout<<"\nStudent names in descending order\n";
for(i = 0; i<=9; i++)
cout<<names[i]<<endl;
}
// definition of sort_asc() function
void sort_asc(char nm[10][20])
{
int i, u;
char temp[20];
u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(strcmp(nm[i],nm[i+1]) > 0)
{
strcpy(temp, nm[i]);
strcpy(nm[i], nm[i+1]);
strcpy(nm[i+1], temp);
}
i++;
}
u--;
}
}

// definition of sort_des() function
void sort_des(char nm[10][20])
{
int i, u;
char temp[20];
u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(strcmp(nm[i],nm[i+1]) < 0)
{
strcpy(temp, nm[i]);
strcpy(nm[i], nm[i+1]);
strcpy(nm[i+1], temp);
}
i++;
}
u--;
}
}

Hope You will learn Something. Happy Coding.

Visit my YouTube Channel as Well๐Ÿ˜‡ https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4

Click on the below Links for more Programming Exercises:

Conditional Structure Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/conditional-structures-exercises/

Array Exercises: ๐Ÿ‘‰https://myustaadg.com/category/programming-exercises/arrays-exercises/

Arrays Exercises, Programming Exercises Tags:Array Exercises, Array exercises in C++, Arrays, arrays codes, Arrays exercises, Arrays in C++, Arrays in C++ Programming with examples, Arrays in Programming, Arrays with example, Display students data in descending order - C++, display them in ascending and descending order., How can I sort the name of student in c++, how do you initialize an array in c++, Java program for bubble sort in Ascending & descending order, Sort students by name in descending order, Structure Sorting (By Multiple Rules) in C++, Write a program that inputs the names of students of a class and display them in ascending, Write a program that inputs the names of students of a class and display them in descending order., Write a program to accept a list of 20 integers Sort, Write a program to input twenty names in an array Arrange, Write a Program to Input Twenty Names in an Array.

Post navigation

Previous Post: Write a program that inputs data into a table and finds out the maximum and minimum values | Searching Maximum and Minimum values from a table.
Next Post: Write a program to enter 10 integers in a single-dimensional array and then print out the array in descending order.

More Related Articles

Write a program that inputs ten values into an array. It finds out the average value of the array Arrays Exercises
Write a program that initializes the values in the one-dimensional array and computes the sum of even values. Arrays Exercises
Write a program that initializes roll numbers and marks of different subjects in a two-dimensional array. Arrays Exercises
Write a program that inputs values into a one-dimensional array of ten elements. Draws a bar chart or histogram. Arrays Exercises
Write a program that reads a positive integer, , entered by the user and then displays on the screen the sum of all integers from 1 to . The sum of the positive first integers can be calculated as follows: Sum =n(n+1)/2 | python Programming Programming Exercises
Write a program that uses three one-dimensional arrays and inputs 20 values in 1st array and differentiates even and odd values. Arrays Exercises

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme