| | |
Array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
Originally Posted by iamnoangel26
Umm i just want to ask how to use an array and when to use them. I just cant get what my teacher is telling. Maybe someone can give me a new idea. That's all thanks
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
It depends on what type of array are you talking about, array of numbers, array of characters, array of strings, array of pointers...
Example:
Say you need to make a program where you will input some numbers and then you will have to sort them.It would be pain in the ass or even impossible to code it without using arrays.
<< moderator edit: added [code][/code] tags >>
This is an example of how and why we use arrays.It's very simple and I hope you got a picture.
Example:
Say you need to make a program where you will input some numbers and then you will have to sort them.It would be pain in the ass or even impossible to code it without using arrays.
C++ Syntax (Toggle Plain Text)
//this is our funcion to display the content of te array.To accept the array we want to send here from main() we must initialize a new array so the program knows what type of information we're talking about, and new int void display(int a[], int s) { //we go through the i (0) till s(size of array, 5) moving i for one up each time for (int i = 0; i < s; i++) { cout << a[i] << " " << endl; //we display the array (a is array and i is its index, from a[0] to a[4] -> because it counts from o not from one } cout << endl; } //we input the our array and its size void sort(int a[], int s) { //we're going to use bubblesort in this example.bubblesort is something like this: imagine a row of number verticaly, it checks from the bottom up two numbers and put the smaller our higher and it does that till the numbers are sorted.I call it passing or checking (I'm not good at english) how many times it will compare two numbers.Now we have 5 numbers and that's four checkings (comparison), that's why we have to do s - 1, the number of numbers minus 1 for (int pass = 0; pass < s -1; pass) { for (int i = 0; i < s - 1; i++) { //this is where we compare two numbers, if the first number is bigger then the next number then change them (call change function) if (a[i] > s[i + 1]) change(a[i], a[a + 1]); } } } //why &? because when we specify & we make the value keep the new value and return that way void change(int& first, int& second) { int temp = first; //we store the first number into temporary int first = second; //then we put the content of the second number to the first one (first one gets the value of second) second = temp; //and now we put the value of temp (which was formerly first) to second and the numbers are changes } int main() { const int size = 5; //this is a size of the array int array[size] = {34, 23, 12, 3, 67}; //we make an array of 5 numbers here we are going to sort later cout << "Data before sorting: "; display(array, size); //we call a function to display the content of an array with two arguments.First one is our array and the second one is the size of the array sort(array, size); cout << "Data after sorting: "; display(array, size); return 0; }
This is an example of how and why we use arrays.It's very simple and I hope you got a picture.
•
•
Join Date: May 2005
Posts: 82
Reputation:
Solved Threads: 1
an array can be defined as a collection of similar data types.
suppose you want to use say 10 to 20 integer variables in your program it would be difficult to declare 20 variables.instead of that you can declare an integer array of size 20.and accessing elements in the array is easy and efficient.ou can simply declare the array by int a[20];
a[0] refers to 1st element,a[1] refers to second element and so on......
similarly a 2-d array is an array of arrays. it is equivalent to a matrix in the sense that in a 2-d array elements are stored in the format of a matrix.
strings are nothing but array of charecters.ok this is just an amateur introduction to arrays.you can find many tutorials on arrays on the web.
just try google.
suppose you want to use say 10 to 20 integer variables in your program it would be difficult to declare 20 variables.instead of that you can declare an integer array of size 20.and accessing elements in the array is easy and efficient.ou can simply declare the array by int a[20];
a[0] refers to 1st element,a[1] refers to second element and so on......
similarly a 2-d array is an array of arrays. it is equivalent to a matrix in the sense that in a 2-d array elements are stored in the format of a matrix.
strings are nothing but array of charecters.ok this is just an amateur introduction to arrays.you can find many tutorials on arrays on the web.
just try google.
•
•
•
•
Originally Posted by indianscorpion2
an array can be defined as a collection of similar data types.
suppose you want to use say 10 to 20 integer variables in your program it would be difficult to declare 20 variables.instead of that you can declare an integer array of size 20.and accessing elements in the array is easy and efficient.ou can simply declare the array by int a[20];
a[0] refers to 1st element,a[1] refers to second element and so on......
similarly a 2-d array is an array of arrays. it is equivalent to a matrix in the sense that in a 2-d array elements are stored in the format of a matrix.
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
>Ok But how can i get the value of the 5th element? does cout<<a[5]; will do?
well,it will ouput the 6'th element, since arrays start on 0.
>And what if i want to output all the elements? do I write cout<<a[0], a[1], a[2]... a[20];
yes, but [20] dont exist, since int a[20] create only 20 elements (0-19)
and you prob want to use a loop to print them out.
like:
well,it will ouput the 6'th element, since arrays start on 0.
>And what if i want to output all the elements? do I write cout<<a[0], a[1], a[2]... a[20];
yes, but [20] dont exist, since int a[20] create only 20 elements (0-19)
and you prob want to use a loop to print them out.
like:
C++ Syntax (Toggle Plain Text)
for (int i; i < 20; i++) { cout << a[i] << '\n'; }
#include <iostream>
using namespace std;
int main()
{
int array[] = {10,20,30,40,50};
for ( int i = 0; i < 5; ++i )
{
cout << "array[" << i << "] = " << array[i] << '\n';
}
return 0;
}
/* my output
array[0] = 10
array[1] = 20
array[2] = 30
array[3] = 40
array[4] = 50
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Apr 2008
Posts: 35
Reputation:
Solved Threads: 0
Hello bsdpowa,
I see what you saying about the sort function, but I try the way you have shown on my code it did't work, I want to use sort function to display the array that I assigned before sort and after sort. Without using sort just indexing it work fine but when I use the sort functin then it didn't work.
What did I do wrong???
I see what you saying about the sort function, but I try the way you have shown on my code it did't work, I want to use sort function to display the array that I assigned before sort and after sort. Without using sort just indexing it work fine but when I use the sort functin then it didn't work.
C++ Syntax (Toggle Plain Text)
#include <iostream> void display(int arr[], int size); int main() { using namespace std; //int arr[13] = {80, 127, 120, 42, 115, 125, 120, 49, 126, 42, 115, 126, 73}; const int size = 13; int arr[size] = {80, 127, 120, 42, 115, 125, 120, 49, 126, 42, 115, 126, 73}; sort(arr, size); cout << "Display numbers in the array" << endl; for(int i=0; i < size; i++) cout << " Integer [arr]: " << arr[i] << endl; //Calculate the average value double total = 0.0; for(int i = 0; i < 13; i++) total += arr[i]; double ave = total / 13; //report the results cout << " Average value = " << ave << endl; //Print numbers above the average cout << "Display number above average" << endl; for(int i=0; i < 13; i++) if(arr[i] > ave) cout << " Integer [arr]: " << arr[i] << endl; //Print numbers below the average cout << "Display number below average" << endl; for(int i=0; i < 13; i++) if(arr[i] < ave) cout << " Integer [arr]: " << arr[i] << endl; cout << "Display characters for the array" << endl; for(int i=0; i < 13; i++) cout << (char)(arr[i]-10); cin.get(); return 0; }
What did I do wrong???
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






