Question

Title Algorithms Functional Requirements and Marks using c++ Write a pseudo-code algorithm for the following problems:...

Title

Algorithms

Functional Requirements and Marks

using c++

Write a pseudo-code algorithm for the following problems:

Detect if three angles can make a triangle.    (1)


Hint: In a triangle, the sum of all angles is 180.


Switch the value of two numbers     (1)


If a=2 and b=5, we want to have a=5 and b=2 at the end.


Remember that a=b will result in the old value to of a to get lost.


Receive a set of numbers and find the max.    (1)


In the previous case (3), switch the value of first number with the max    (1)


Use what you did in (4) and sort the set of numbers.    (1)


Max value goes to the first data, then max of remaining values goes to the second, etc.


Implementation Requirements

Start each algorithm with a list of data items it uses.


In your algorithm show where each data item get its value (input from user, read from database, etc)


Use comments and description if necessary, to explain what your algorithm does.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for further help. Please uprate.

Code:

// DifferentAlgorithms.cpp : Defines the entry point for the console application.
//
#include<iostream>
using namespace std;


//checking valid traingle
/* Count how many angles given will summing them up.
If you weren't given just three angles or if they don't all add up to 180, then it cannot make up a triangle.
*/
void validTriangle(){
   int angle1,angle2,angle3,sum;
   cout<<"Please give angles :\n";
   cin>>angle1;
   cin>>angle2;
   cin>>angle3;
   sum=angle1+angle2+angle3;
   if(sum==180 && angle1>0 && angle2>0 && angle3>0){
   cout<<"Triangle is valid.\n";
   }else{
       cout<<"Triangle is invalid.\n";
   }
}

/* first adding to get sum
then subtract second from sum so we get first.
subtract first from sum so we get second*/
void switchTwoNumbers(int &first,int &second){
   first=first+second;
   second=first-second;
   first=first-second;
}

/*
set variable highest equal to the first element in the array.
walk the array starting with the 2nd element.
If an entry is higher than highest, set highest to the value
*/
int findMax(int list[],int start,int size){
   int max=list[start];
   for(int i=start;i<size;i++){
       if(max<list[i]){
       max=list[i];
       }
   }
   return max;
}

int getMaxIndex(int list[],int start,int size){
   int index=start;
   int max=list[index];
   for(int i=start;i<size;i++){
       if(max<list[i]){
       index=i;
       max=list[i];
       }
   }
   return index;
}

/* walk array starting with the 2nd element. if an entry is higher than array[0] use the swap in #2 to switch them*/
void setFirstAsMax(int list[],int size){
   int index=getMaxIndex(list,0,size);
   switchTwoNumbers(list[0],list[index]);
}

/* looping through array and swapping highest index with current index*/
void sort(int list[],int size){
   int index;
   for(int i=0;i<size;i++){
       index=getMaxIndex(list,i,size);
       if(index!=i)
       switchTwoNumbers(list[i],list[index]);
   }
}

/* Taking input from console*/
void main(){
   cout<<"1) Testing Valid Triangle:\n";
   validTriangle();
   /* Swapping two number */
   cout<<"\n2) Swapping Numbers:\n";
   int first,second;
   cout<<"Please give two numbers.\n";
   cin>>first;
   cin>>second;
   cout<<"Numbers before swapping : First "<<first<<"\tSecond "<<second<<endl;
   switchTwoNumbers(first,second);
   cout<<"Numbers after swapping : First "<<first<<"\tSecond "<<second<<endl;

   /* Finding Max */
   cout<<"3) Finding Max:\n";
   int list[10];
   cout<<"Please give 10 numbers\n";
   for(int i=0;i<10;i++){
       cin>>list[i];
   }
   int max = findMax(list,0,10);
   cout<<"Maximum Value is :"<<max<<endl;

   /* Setting First As Max */
   setFirstAsMax(list,10);
   cout<<"List after swapping with max:"<<endl;
   for(int i=0;i<10;i++){
       cout<<list[i]<<endl;
   }

   /* Sorting using Max */
   sort(list,10);

   cout<<"List after sorting:"<<endl;
   for(int i=0;i<10;i++){
       cout<<list[i]<<endl;
   }
   system("pause");
}

Output:

Add a comment
Know the answer?
Add Answer to:
Title Algorithms Functional Requirements and Marks using c++ Write a pseudo-code algorithm for the following problems:...
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
  • Convert the Convergent-Pointer Algorithm pseudo code to C code. Use exact variables and make no modifications....

    Convert the Convergent-Pointer Algorithm pseudo code to C code. Use exact variables and make no modifications. Get values for n and the n data items Set the value of legit to n Set the value of left to 1 Set the value of right to n While left is less than right do 1 If the item at position left is not 0 then Increase left by 1 Otherwise Reduce legit by 1 Copy the item at position right into...

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • Create a complete pseudo-code program in C++ to do the following using the PseudoCode language developed...

    Create a complete pseudo-code program in C++ to do the following using the PseudoCode language developed in class using Absolute Addressing. There is 1 deliverable: 1. You should include the input cards shown below as a test. THE PROGRAM: First you will read in a value N which holds the number of values to be read in. (so if N is 20 then there will be 20 more cards to read in.) Read in N values into an array. Bubble-Sort...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should...

    Assignment on Java programing 1. Write programs for the following exercises in Java. Each file should have short description of the implemented class and for files with main method the problem it is solving. Make sure your files have appropriate names. Programs should write output to the Console. b) BST: Implement Binary Search Tree ADT with insert(int key), delete(int key), Node find(int key), and in-order traverse() where it prints the value of the key. Your operations should use recursion. The...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • In C++ language, implement a class that can sort an array of numbers using all three...

    In C++ language, implement a class that can sort an array of numbers using all three algorithms we have seen in this course, but each method updates a “counter” value every time it accesses the array. Have it print this at the end of the sorting process. Store the array values in an “original” array so you don’t have to re-type it for different sorts (since each sort alters the array), and have the sort modify a copy. Note: IF...

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

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
ADVERTISEMENT