Urgent help needed for an assignment
skyfirestalker (4)
Dec 30, 2012 at 1:46pm UTC
Hey Guys,
Im workin on an assignment to be handed over soon and Im really stuck with identifying sequence, selection and iteration in the following code. Could someone identify these three for me? And a simple explanation of why they are used in this instance would be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
string nameArray[10]; //to store student names
int markArray[10]; //to store student marks
void ExchangeSort()//sorting the marks in descending order using exchange sort
{
int i, j, ans;
string ans2;
int temp; // temporary holding variable
int numLength = 10;
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
if (markArray[j]<markArray[j+1])
{
ans=markArray[j+1];
markArray[j+1]=markArray[j];
markArray[j]=ans;
}
{
ans2=nameArray[j+1];
nameArray[j+1]=nameArray[j];
nameArray[j]=ans2;
}
}
}
for (int i=0; i<=10; i++)
{
cout<<markArray[i]<<" - " ;
cout<<nameArray[i]<<endl;
}
}
float calculateAverage(){ //averagecalculation
float sum = 0;
for (int i=0; i<10; i++){
sum = sum + markArray[i]; // add all marks
}
float average = sum/10; // calculate average
cout << "The average mark is : " << average <<endl;
return average;
}
void calculateAboveAverage(){
float avg = calculateAverage(); // get the average value by calling a function
for (int j=0; j<10; j++){
if (markArray[j] > avg){ //check what values are greater than average value
cout << nameArray[j] << " " << markArray[j] << endl;
}
}
}
void displaynames(){ //to display the names, marks and the grading
string seek;
int i,x;
cout<< "Enter a student's name" <<endl;
cin >> seek;
for (i=0;i<10;i++){
if (nameArray[i]==seek){
if (markArray[i]>=75 && markArray[i]<=100){
cout << nameArray[i]<< " " <<markArray[i]<< " D" <<endl;
}
if (markArray[i]>=65 && markArray[i]<=74){
cout << nameArray[i]<< " " <<markArray[i]<< " M" <<endl;
}
if (markArray[i]>=40 && markArray[i]<=64){
cout<< nameArray[i]<< " " <<markArray[i]<< " P" <<endl;
}
if (markArray[i]<40){
cout<< nameArray[i]<< " " <<markArray[i]<< " F" <<endl;
}
}
}
}
void main()
{
int sum = 0;
cout <<"Student 01" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[0];
cout<<"Enter Name: " ;
cin >> nameArray[0];
cout <<"Student 02" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[1];
cout<<"Enter Name: " ;
cin >> nameArray[1];
cout <<"Student 03" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[2];
cout<<"Enter Name: " ;
cin >> nameArray[2];
cout <<"Student 04" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[3];
cout<<"Enter Name: " ;
cin >> nameArray[3];
cout <<"Student 05" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[4];
cout<<"Enter Name: " ;
cin >> nameArray[4];
cout <<"Student 06" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[5];
cout<<"Enter Name: " ;
cin >> nameArray[5];
cout <<"Student 07" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[6];
cout<<"Enter Name: " ;
cin >> nameArray[6];
cout <<"Student 08" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[7];
cout<<"Enter Name: " ;
cin >> nameArray[7];
cout <<"Student 09" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[8];
cout<<"Enter Name: " ;
cin >> nameArray[8];
cout <<"Student 10" <<endl;
cout<<"Enter marks:" ;
cin >>markArray[9];
cout<<"Enter Name: " ;
cin >> nameArray[9];
bool flag = true ;
while (flag){
int option; // storing the option user selected
float avg;
cout << "What do you want to do? Please enter a number to choose from the below" <<endl;
cout << "1. Display the students' marks and corresponding names in descending order" << endl;
cout << "2. Display the avergae marks" << endl;
cout << "3. Display the names of students who have marks above the average value" << endl;
cout << "4. Search for a student" <<endl;
cin >> option;
switch (option){
case 1: ExchangeSort();
break ;
case 2:
avg = calculateAverage();
break ;
case 3: calculateAboveAverage();
break ;
case 4: displaynames();
break ;
default :flag = false ;
cout<< "Enter a number from 1 to 4" <<endl;
break ;
}
}
_getch();
}
goth (47)
Dec 30, 2012 at 2:30pm UTC
I don't understand what you mean by 'sequence' but I can help you with the last two.
selection: the code starting at line 182 and ending at 203 and any other if...else in your code. It's a "selection" because it selects from several cases (if(condition) <code> else <some_other_code>; and switch being just multiple if...else's)
iteration: to iterate means to go through every element of a structure and either look at it or do some operation on it. The code inside ExchangeSort(), calculateAverage(), calculateAboveAverage() and displaynames() does this.
skyfirestalker (4)
Dec 30, 2012 at 2:48pm UTC
@goth, thanks alot for the great help!
selection and iteration are clear enough for me. i simply didnt undrstand wat sequence was either. it was only mentioned in the question. dat doesnt matter.
Thank you very much for your help and time!!!! :)
Chervil (1205)
Dec 30, 2012 at 4:29pm UTC
Sequence is the most basic of all. It is simply a group of instructions which are executed in a linear fashion, one after another. The flow of control simply steps through the code one line at a time.
Last edited on Dec 30, 2012 at 4:46pm UTC
Topic archived. No new replies allowed.