473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

c++ script help

Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <ios>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8. int main ()
  9. {
  10. cout <<" plz enter your first name: ";
  11. string name;
  12. cin>>name;
  13. cout<<"Hello, "<<name<<"!"<<endl;
  14. cout<<plz enter ur midterm and final exam grades: ";
  15. double midterm, final;
  16. cin>>midterm>>final;
  17. cout<<"enter all ur homeworks grades, :
  18. "followed by end-of-file: ";
  19. vector<double> homework;
  20. double x;
  21. while (cin>>x)
  22. homework.push_back(x);
  23. typedef vector<double>::size_type vec_sz;
  24. vec_sz size= homework.size();
  25. if(size==0) {
  26. cout<<endl<<"u must enter ur grades. "
  27. "plz try again."<<endl;
  28. return1; // does that mean if this statement fail it restart the if statement or the whole app
  29. sort(homework.begin(), homework.end() );
  30. vec_sz mid=size/2
  31. double median;
  32. median=size %2 ==0 ? (homework[mid]+homework[mid-1] /2
  33. :homework[mid];
  34. //isn't homework[mid]= to homework homework[size/2] which is how many numbers are contain in homework
  35. //just how does it works don't very get it
  36. streamsize prec=cout.precison(3)
  37. <<0.2+midterm+0.4 *final+0.4*median//isn't median =size%2
  38. <<setprecison(prec)< <end;
  39. return0;
  40. }
  41.  
I have read the book seven times, but still don't get it.
I know it sounds fishy, but I very need ur help.
Ur voice are super important to me.
Plz lend me a hand
Sep 2 '07 #1
20 1910
Studlyami
464 Expert 256MB
What problems are you having? are you getting an error message.? A little more information please.
Sep 2 '07 #2
no i just don't understand the script. Plz help
#
return1; // does that mean if this statement fail it restart the if statement
line 28
line 32-34
#
median=size %2 ==0 ? (homework[mid]+homework[mid-1] /2
#
:homework[mid];
#
//isn't homework[mid]= to homework homework[size/2] which is how many numbers
and line 37
<<0.2+midterm+0.4 *final+0.4*median//isn't median =size%2
Sep 2 '07 #3
kreagan
153 100+
no i just don't understand the script. Plz help
return1; // does that mean if this statement fail it restart the if statement
no no. I means that if no homeworks were inputed, stop the program. The return will break out of main. Basically shutting down your program.

median=size %2 ==0 ? (homework[mid]+homework[mid-1] /2
:homework[mid];
To take the median, you need to find the middle test. However, if there is an even number of tests, which one is the middle. Here's a visual example:

1 2 3 4 5 < the median is 3
1 2 3 4 < the median is (2 + 3)/2

This checks the size, if the size is even, do first command (between question mark and colon), if not, do what is after the colon. Basically it is a boolean if statement.

Hope that helped.
Sep 2 '07 #4
median=size %2 ==0 ? (homework[mid]+homework[mid-1] /2
:homework[mid];
sorry i still don't get it.
I get the non-pc version how to compute median.
Like if it is even divide the size /2
for example
10/2
the median will be 5 number +6 number /2
And the odd number is size/2
then the number to that is the median.
But the problem is isn't
median=size %2 ==0 ? (homework[mid]+homework[mid-1] /2
:homework[mid];
only calculate where is the median located ,but not the median itself???????
Sep 2 '07 #5
Studlyami
464 Expert 256MB
wow i must be slow 2 replies before i finished.

median will equal the value located in the homework vector
or it will equal the average of the two middle values in the homework vector.
Sep 2 '07 #6
Err don't quite get what do u mean by that.
Sorry about that.
Sep 2 '07 #7
Studlyami
464 Expert 256MB
okay

homework is a vector that contains doubles
if (size % 2 == 0) //there are an even number of grades
median = (homework[mid] + homework[mid-1] / 2) // (double + double) / 2
else // there are an odd number of grades
median = homework[mid] //equals the double value located in the middle of the vector
Sep 2 '07 #8
JosAH
11,448 Expert 8TB
Maybe it's because it's Sunday overhere but I find this thread very chaotic.
Suppose the homework[] array is sorted in ascendng or descending order. Let the
number of elements in that homework[] array be 'n'.

There are just two cases:

1) n is odd; the median is homework[n/2];
2) n is even; the median is (homework[n/2]+homework[n/2+1])/2;

if n&1 == 1 then n is an odd number; there's not much more to it.

kind regards,

Jos
Sep 2 '07 #9
o so because size is vector double
Which means it actually contain the number itself and not how many numbers there is right???
It is Sunday, I know I am sorry for bugging u in Sunday.
Sep 2 '07 #10
any answer. Plz i need ur hand.
Sep 2 '07 #11
JosAH
11,448 Expert 8TB
o so because size is vector double
Which means it actually contain the number itself and not how many numbers there is right???
It is Sunday, I know I am sorry for bugging u in Sunday.
You're not 'bugging' me; I'm just being lazy but you are over complicating matters.
What does 'actually contains the number itself' mean? Here's an example:

12, 14, 20, 100, 1000

There are five numbers, so n == 5 and n/2 == 2 (integer division which means
skip the remainder). C and C++ indexes start counting from 0 so, element #2
is the median, i.e. 20 is the median.

Another example:

12, 14, 20, 100

There are four numbers, so n== 4 and n/2 == 2 so element #2 and 2-1 == #1
make up the median (20+14)/2 == 17 is the median.

kind regards,

Jos

ps. I made a mistake in my previous reply (me being lazy); change the n/2+1
to n/2-1 and everything is fine again.
Sep 2 '07 #12
so size is actually
for example if i input these number.
1,2,3,4
size will contain 1,2,3,4
instead of just 4 like counting how many numbers a there
Sep 2 '07 #13
JosAH
11,448 Expert 8TB
so size is actually
for example if i input these number.
1,2,3,4
size will contain 1,2,3,4
instead of just 4 like counting how many numbers a there
I'm sorry, I really don't understand what you're talking about; e.g. these numbers

10, 20, 30, 40

The size of the array/vector/whatever is still 4 but the number 4 isn't one of the
elements off the array/vector/whatever. The median will be the average of the
numbers stored at position 4/2 and 4/2-1 so (30+20)/2 == 25.

kind regards,

Jos
Sep 2 '07 #14
4/2 + 4/2-1 /2
because of order of operation so is the ((4/2+4/2)-1)/2
which is 2+2-1 which is three then divided by 2.
but that is the first number plus second /2.
not second number +the third one??????
Sep 2 '07 #15
JosAH
11,448 Expert 8TB
4/2 + 4/2-1 /2
because of order of operation so is the ((4/2+4/2)-1)/2
which is 2+2-1 which is three then divided by 2.
but that is the first number plus second /2.
not second number +the third one??????
If there are four (4) numbers to be considered, take the 4/2 and 4/2-1, which are
the 2nd and 1st numbers and take the average of them. Remember C and C++
start counting array elements from zero: the 0th, 1st, 2nd, 3rd, 4th, 5th element etc.

kind regards,

Jos
Sep 2 '07 #16
so is the fifth number divided by 2 but rounded up the decimal.
so is 5/2 +5/2-1 then divided by 2
so is 2.5 + 1.5/2
but since they rounded up it is 3+2 /2 right?????
Sep 2 '07 #17
plz come back and help.
I need ur help plz.
Sep 3 '07 #18
or is it 4/2+1 and 4/2-1+1 then divided by two.
Sep 3 '07 #19
JosAH
11,448 Expert 8TB
or is it 4/2+1 and 4/2-1+1 then divided by two.
What are you juggling? Here's another example:

0:10
1:20
2:30
3:40
4:60

The number left of the colon is the index of the number, right of the colon is
the number itself. This array has 5 elements, so 5/2 == 2 so that median is
found at index number 2, which is 30.

For an even number of elements:

0:10
1:20
2:30
3:40

There are four elements so take the average of the numbers at indexes 4/2
and 4/2-1, which are index values 2 and 1 so take the average of 30 and 20
which makes (30+20)/2 == 25.

In general: if n (the number of elements in the array) is odd, take the
element at index value n/2; if n is even take the average of the two numbers
at index positions n/2 and n/2-1. Of course the numbers in the array need
to be sorted, either ascending or descending.

kind regards,

Jos
Sep 3 '07 #20
I get it now thx
for ur great help
Sep 3 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
4
by: hupjack | last post by:
I finally joined the millions of cell phone users out there. I'm the 2nd phone on what is now a family share plan. (Our two cell phones use minutes from a central 400 minute peak time pool.)...
14
by: Akbar | last post by:
Hey there, Big-time curiosity issue here... Here's the test code (it's not that long)... it's to display a large number of image links with captions, ideally pulled in from an external file...
8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
4
by: Derek | last post by:
I have the following script in a page and it gets an error in IE 6. Says something about an invalid argument but the line number doesn't help since I can't see the javascript code when viewing...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
9
by: Harry Smith | last post by:
While reading the documentation on IsStartupScriptRegistered, there is a reference to "client startup script" as "Determines if the client startup script is registered with the Page object." What...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
3
by: David | last post by:
On Sun, May 4, 2008 at 4:43 AM, lev <levlozhkin@gmail.comwrote: Hi, I started tidying up the script a bit, but there are some parts I don't understand or look buggy. So I'm forwarding you the...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.