![]() |
| | #1 |
| Registered User Join Date: Oct 2006
Posts: 37
| Also I'm very sure that arrays will be included, because the last topic that we learnt was arrays and recently we had a mock C++ test which included arrays. And so far, when it comes to arrays, I'm totally clueless. The reason is because my lecturer is useless in teaching. He's is just as good as, "Here we will be learning arrays. The end. Learn everything yourself." But of course he does try to teach, but nobody in class knows whats going on. So I've have found a few good web sites on C++ and have a book on C++, but I'm only starting to learn arrays. I have the simple basic for C++, but still, I have no idea on how to use arrays. So while I'm trying to learn it, I taught that may be I could post here and ask for advices and a bit of lessons. Could anyone give me a simple example of a code using arrays? How about, arranging numbers from smallest to biggest? I will scan my mock test and post it here later, so that you guys have a better Idea of the test. And since its an open book test, we are allowed to have notes during the test, whether on paper or even on the internet. So would it be okay if I log on this site during the test? But no cheating of course... May be to help me out a bit? |
| [Z-D] is offline | |
| | #2 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,275
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Oct 2006
Posts: 16
| I think that if he can't understand arrays at this point, then sorting algorithms may be a little beyond his current abilities. For arrays, it's really quite simple if you understand variables. All that an array is is a variable. so what makes an array different, you ask? Well, for one thing, arrays are more than one variable. Here's the example that I've seen used numerous times when describing arrays: Suppose that you have six numbers. Each one is the id of an object. So we'd have Code: int id1, id2, id3, id4, id5, id6; id1 = 0; id2 = 1; id3 = 5; id4 = 7; id5 = 2; id6 = 4; Code: int id[6]; id[0] = 0; id[1] = 1; id[2] = 5; id[3] = 7; id[4] = 2; id[5] = 4; What's the point of this? At first, there doesn't seem to be much of any. However, there are several uses, one of the most common uses is that arrays and for loops are built for each other. Instead of having to do: Code: cout << id1 << "\n"; cout << id2 << "\n"; coud << id3 << "\n" ... Code: for(int i = 0; i < 6; i++) cout << id[i] << "\n"; |
| therabidwombat is offline | |
| | #4 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| There's a tutorial on arrays, I don't know if you've seen it: http://www.cprogramming.com/tutorial/lesson8.html Basically, an array is a convenient name for many different variables as the previous poster has indicated.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #5 |
| Registered User Join Date: Oct 2006
Posts: 37
| Thanks alot guys,.... I've been reading on this a lot for the past don't know how many hours. I mannage to understand a bit. laserlight, I went through that site but It was kind of hard to understand completely. About the Bubble sort code, Code: void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
therabidwombat: Thanks a lot! I mannaged to understand the basic already. But what I noticed in the lecture slides provided by the lecturer is that all the values in the arrays are all fixed. For example Code: const int responseSize = 99;
response[ responseSize ] =
{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
4, 5, 6, 1, 6, 5, 7, 8, 7 };
There is more which I don't understand. Well actually the code starts with this: Code: #include <iostream.h>
#include<iomanip.h>
void mean(const int[], int);
void median(int[], int)
void mode(int[], int[], int)
void bubbleSort(const int[], int);
int main()
{
const int responseSize = 99;
response[ responseSize ] =
{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
4, 5, 6, 1, 6, 5, 7, 8, 7 };
mean( response, responseSize );
median( response, responseSize );
mode( frequency, response, responseSize );
return 0;
}
...
...
here's the code for defining median: Code: void median( int answer[], int size )
{
cout << "\n********\n Median\n********\n"
<< "The unsorted array of responses is";
printArray( answer, size );
bubbleSort( answer, size );
cout << "\n\nThe sorted array is";
printArray( answer, size );
cout << "\n\nThe median is element " << size / 2
<< " of\nthe sorted " << size
<< " element array.\nFor this run the median is "
<< answer[ size / 2 ] << "\n\n";
Code: ******** Median ******** The unsorted array of responses is 6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3 5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8 7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7 The sorted array is 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 The median is element 49 of the sorted 99 element array. For this run the median is 7 Code: printArray( answer, size ); and, to dwks: Yeah I did check that out. The explanation seems to be too general and less technical. Thanks anyway. |
| [Z-D] is offline | |
| | #6 |
| Registered User Join Date: Oct 2006
Posts: 37
| Wait, wait.... May be I got it. Is this how you input the values? Code: int main();
{
int grades[10]
for (int i = 0; i < 4; 4; i++)
{
cout << "Enter 10 grades: ";
cin >> grades[i];
}
|
| [Z-D] is offline | |
| | #7 |
| Registered User Join Date: May 2006
Posts: 894
| Your for() loop is wrong, it should read for(int i = 0; i < 10; i++). There also lacks a closing brace and you have an extra semi-colon after int main(). Oh, and you lack a semi-colon after int grades[10]; |
| Desolation is offline | |
| | #8 |
| Registered User Join Date: Oct 2006
Posts: 37
| Oh yeah... mistakes. Thanks. So it should be: Code: int main();
{
int grades[10];
for(int i = 0; i < 10; i++);
{
cout << "Enter 10 grades: ";
cin >> grades[i];
}
...
...
...
return 0;
}
|
| [Z-D] is offline | |
| | #9 |
| Its hard... But im here Join Date: Apr 2005 Location: England
Posts: 1,467
| >int main(); Still got a semi-colon here
__________________ I'm just trying to be a better person - My Name Is Earl |
| swgh is offline | |
| | #10 | |
| Registered User Join Date: May 2006
Posts: 894
| Quote:
If you are wondering how the bubble sort works well you can take a sheet of paper and a pencil and work by hand what it's doing. You'll learn a lot more. I find it's a twisted version of the bubble sort, though. The way I know it is much simpler. Whatever. Code: void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}
Code: for ( all entries in the array, going backwards )
{
for ( all the entries from 1 to i )
{
if( the entry at the location j - 1 ( 0, 1 , 2 ... i ) is greater than the entry next to it)
{
switch the values
}
}
}
Edit: Just in case you're visual: This is the list to sort: 4 7 1 5 3 Here's what happens Code: j i
4 7 1 5 3
You always have to j - 1 to j and switch if j - 1 is greater than j
j i
4 7 1 5 3
j i
4 1 7 5 3
ji (both on 3)
4 1 5 7 3
j i
4 1 5 3 7
j i
1 4 5 3 7
ji (both on 3)
1 4 5 3 7
j i
1 4 3 5 7
ji (both on 3)
1 4 3 5 7
1 3 4 5 7
Last edited by Desolation; 11-25-2006 at 09:05 PM. | |
| Desolation is offline | |
| | #11 | ||||
| Registered User Join Date: Oct 2006
Posts: 37
| Quote:
Quote:
Quote:
Code: i>=1 So if I do this, Code: for (i = (array_size -1); i >=0; i--)
{
for (j = 0; j <=i; i++)
Quote:
So will this work? Code: int main()
{
int numbers[10];
for (int i = 0; i < 10; i++);
{
cout << "Enter 10 grades: ";
cin >> numbers[i];
cout << "Your sorted grades are: ";
bubbleSort(int numbers[], int array_size);
}
return 0;
}
void bubbleSort{int numbers[], int array_size)
{
int i, j, grades;
for (i = (array_size -1); i >=0; i--)
{
for (j = 0; j <=i; i++)
{
if (numbers[j-1] > numbers[j])
{
grades = numbers[j-i]
numbers[j-1] = numbers [j];
number[j] = grades;
}
}
}
}
This is the output that I'm trying acheive: Code: Out put: Enter 10 grades: 15, 24, 12, 99, 58, 47, 26, 45, 87, 55 <---- then we enter grades here Your sotrt grades are: 12, 15, 24, 26, 45, 47, 55, 58, 87, 99 <----- this is the sorted one Last edited by [Z-D]; 11-26-2006 at 02:10 AM. | ||||
| [Z-D] is offline | |
| | #12 |
| Registered User Join Date: May 2006
Posts: 894
| 1. You were wrong when you said the sorting was done by printArray(). 2. No, changing j = 1 to j = 0 will crash your program. Try to find why, as an exercise. 3. Not changing i >= 0 will simply leave you with an useless for() loop executed. You have to change it to i >= 1 to get rid of it because then when i equals 1, the second for() loop will get executed as well (since it is executed for j <= i). |
| Desolation is offline | |
| | #13 |
| Registered User Join Date: Oct 2006
Posts: 37
| Okay then. But how do we display the arrange set of numbers in the output? And supposing I want the programme to be able to input 10 numbers(or grades) then arrange and output the numbers. How do set it so that "i" must be between 0 and 100? Meaning equals or more than 0 and equals or less than 100. Was my code in my previous code correct? I couldn't seem to get it right. Okay so this is from the lecturers notes... Code: #include <iostream.h>
#include<iomanip.h>
void mean(const int[], int);
void median(int[], int)
void mode(int[], int[], int)
void bubbleSort(const int[], int);
int main()
{
const int responseSize = 99;
response[ responseSize ] =
{ 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
4, 5, 6, 1, 6, 5, 7, 8, 7 };
mean( response, responseSize );
median( response, responseSize );
mode( frequency, response, responseSize );
return 0;
}
void mean( const int answer[], int arraySize )
{
int total = 0;
cout << "********\n Mean\n********\n";
for ( int j = 0; j < arraySize; j++ )
total += answer[ j ];
cout << "The mean is the average value of the data\n"
<< "items. The mean is equal to the total of\n"
<< "all the data items divided by the number\n"
<< "of data items (" << arraySize
<< "). The mean value for\nthis run is: "
<< total << " / " << arraySize << " = "
<< setiosflags( ios::fixed | ios::showpoint )
<< setprecision( 4 )
<< static_cast< double >( total ) / arraySize << "\n\n";
}
void median( int answer[], int size )
{
cout << "\n********\n Median\n********\n"
<< "The unsorted array of responses is";
printArray( answer, size );
bubbleSort( answer, size );
cout << "\n\nThe sorted array is";
printArray( answer, size );
cout << "\n\nThe median is element " << size / 2
<< " of\nthe sorted " << size
<< " element array.\nFor this run the median is "
<< answer[ size / 2 ] << "\n\n";
}
void mode( int freq[], int answer[], int size )
{
cout << "\n********\n Mode\n********\n";
int rating, largest = 0, modeValue = 0;
for ( rating = 1; rating <= 9; rating++ )
freq[ rating ] = 0;
for ( int j = 0; j < size; j++ )
++freq[ answer[ j ] ];
cout << "Response"<< setw( 11 ) << "Frequency"
<< setw( 19 ) << "Histogram\n\n" << setw( 55 )
<< "1 1 2 2\n" << setw( 56 )
<< "5 0 5 0 5\n\n";
for ( rating = 1; rating <= 9; rating++ )
cout << setw( 8 ) << rating << setw( 11 )
<< freq[ rating ] << " ";
if ( freq[ rating ] > largest )
{
largest = freq[ rating ];
modeValue = rating;
}
for ( int h = 1; h <= freq[ rating ]; h++ )
cout << '*';
cout << '\n';
}
cout << "The mode is the most frequent value.\n"
<< "For this run the mode is " << modeValue
<< " which occurred " << largest << " times." << endl;
}
void bubbleSort( int a[], int size )
{
int hold;
for ( int pass = 1; pass < size; pass++ )
for ( int j = 0; j < size - 1; j++ )
if ( a[ j ] > a[ j + 1 ] )
{
hold = a[ j ];
a[ j ] = a[ j + 1 ];
a[ j + 1 ] = hold;
}
}
void printArray( const int a[], int size )
{
for ( int j = 0; j < size; j++ )
{
if ( j % 20 == 0 )
cout << endl;
cout << setw( 2 ) << a[ j ];
}
}
Code: //Output:
********
Mean
********
The mean is the average value of the data
items. The mean is equal to the total of
all the data items divided by the number
of data items (99). The mean value for
this run is: 681 / 99 = 6.8788
********
Median
********
The unsorted array of responses is
6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8
7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7
The sorted array is
1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5
5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
The median is element 49 of
the sorted 99 element array.
For this run the median is 7
********
Mode
********
Response Frequency Histogram
1 1 2 2
5 0 5 0 5
1 1 *
2 3 ***
3 4 ****
4 5 *****
5 8 ********
6 9 *********
7 23 ***********************
8 27 ***************************
9 19 *******************
The mode is the most frequent value.
For this run the mode is 8 which occurred 27 times.
What are the mistakes in this code? Also, I'm tyring to understand it, but there is some part which I have difficulty understanding. What I'm interested in is how the the numbers got sorted. Also I don't understand almost the whole part of defining mean. I'm trying to do something like this, where the programme can sort the numbers, but the numbers are input-ed instead of given. This is what I came up with so far. I'm just trying to sort the inputed numbers here. Would this work. Code: #include <iostream.h>
#include<iomanip.h>
void bubbleSort(int numbers[], int array_size); //initialise arrays
int main(void) //call fucntion
{
int numbers[10];
for (int i = 1; i < 10; i++);
{
cout << "Enter 10 grades: ";
cin >> numbers[i];
cout << "Your sorted grades are: ";
bubbleSort(int numbers[], int array_size);
}
return 0;
}
void bubbleSort(int numbers[], int array_size) //defining bubleSort
{
int i, j, grades;
for (i = (array_size -1); i >=1; i--)
{
for (j = 1; j <=i; i++)
{
if (numbers[j-1] > numbers[j])
{
grades = numbers[j-i]
numbers[j-1] = numbers [j];
number[j] = grades;
}
}
}
}
Last edited by [Z-D]; 11-27-2006 at 09:51 AM. |
| [Z-D] is offline | |
| | #14 |
| Registered User Join Date: Mar 2002
Posts: 1,595
| >>What are the mistakes in this code? After a quick look through, there should be a { between the following two lines: Code: for ( rating = 1; rating <= 9; rating++ ) cout << setw( 8 ) << rating << setw( 11 ) Code: for ( int h = 1; h <= freq[ rating ]; h++ ) cout << '*'; cout << '\n'; Code: for ( int h = 1; h <= freq[ rating ]; h++ )
cout << '*';
cout << '\n';
>>Would this work Run it and find out. The big problem I see is you are sorting with every input, which is time intensive. Moving these two lines out of the loop may be better. cout << "Your sorted grades are: "; bubbleSort(int numbers[], int array_size);
__________________ You're only born perfect. |
| elad is offline | |
| | #15 | |||
| Registered User Join Date: Oct 2006
Posts: 37
| Quote:
I better buy one. Quote:
Code: void bubbleSort(int numbers[], int array_size) //defining bubleSort
{
int i, j, grades;
for (i = (array_size -1); i >=1; i--)
{
for (j = 1; j <=i; i++)
{
if (numbers[j-1] > numbers[j])
{
grades = numbers[j-i]
numbers[j-1] = numbers [j];
number[j] = grades;
}
}
}
}
Quote:
Code: int main(void) //call fucntion
{
int numbers[10], i, j;
for (int i = 1; i < 10; i++);
{
cout << "Enter 10 grades: ";
cin >> numbers[i];
}
cout << "Your sorted grades are: ";
bubbleSort(int numbers[], int array_size);
return 0;
}
| |||
| [Z-D] is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Integer Emulation | Elysia | C++ Programming | 31 | 03-18-2008 01:03 PM |
| undefined reference | 3saul | Linux Programming | 12 | 08-23-2006 05:28 PM |
| C++ Operator Overloading help | Bartosz | C++ Programming | 2 | 08-17-2005 12:55 PM |
| MSVC Template Constructor/Assignment Errors | LuckY | Windows Programming | 3 | 07-22-2005 02:57 PM |
| Why is my program freezing? | ShadowMetis | Windows Programming | 8 | 08-20-2004 03:20 PM |