Array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 27
Reputation: iamnoangel26 is an unknown quantity at this point 
Solved Threads: 0
iamnoangel26's Avatar
iamnoangel26 iamnoangel26 is offline Offline
Light Poster

Array

 
0
  #1
Aug 10th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: Array

 
0
  #2
Aug 10th, 2005
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array

 
0
  #3
Aug 10th, 2005
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
A good hint might be when you are naming your variables item1, item2, item3, ... Or if you recognize other patterns of repitition.
"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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 21
Reputation: bsdpowa is an unknown quantity at this point 
Solved Threads: 0
bsdpowa's Avatar
bsdpowa bsdpowa is offline Offline
Newbie Poster

Re: Array

 
0
  #4
Aug 10th, 2005
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.

  1. //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
  2. void display(int a[], int s)
  3. {
  4. //we go through the i (0) till s(size of array, 5) moving i for one up each time
  5. for (int i = 0; i < s; i++)
  6. {
  7. 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
  8. }
  9. cout << endl;
  10. }
  11.  
  12. //we input the our array and its size
  13. void sort(int a[], int s)
  14. {
  15. //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
  16. for (int pass = 0; pass < s -1; pass)
  17. {
  18. for (int i = 0; i < s - 1; i++)
  19. {
  20. //this is where we compare two numbers, if the first number is bigger then the next number then change them (call change function)
  21. if (a[i] > s[i + 1])
  22. change(a[i], a[a + 1]);
  23. }
  24. }
  25. }
  26.  
  27. //why &? because when we specify & we make the value keep the new value and return that way
  28. void change(int& first, int& second)
  29. {
  30. int temp = first; //we store the first number into temporary int
  31. first = second; //then we put the content of the second number to the first one (first one gets the value of second)
  32. second = temp; //and now we put the value of temp (which was formerly first) to second and the numbers are changes
  33. }
  34.  
  35. int main()
  36. {
  37. const int size = 5; //this is a size of the array
  38. int array[size] = {34, 23, 12, 3, 67}; //we make an array of 5 numbers here we are going to sort later
  39.  
  40. cout << "Data before sorting: ";
  41. 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
  42.  
  43. sort(array, size);
  44.  
  45. cout << "Data after sorting: ";
  46. display(array, size);
  47.  
  48. return 0;
  49. }
<< 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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 82
Reputation: indianscorpion2 is an unknown quantity at this point 
Solved Threads: 1
indianscorpion2 indianscorpion2 is offline Offline
Junior Poster in Training

Re: Array

 
0
  #5
Aug 10th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 27
Reputation: iamnoangel26 is an unknown quantity at this point 
Solved Threads: 0
iamnoangel26's Avatar
iamnoangel26 iamnoangel26 is offline Offline
Light Poster

Re: Array

 
0
  #6
Aug 10th, 2005
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.
So you say that instead of declaring variable as int 1, int 2 int 3... int 20; i can declare it buy just int [20];. Ok But how can i get the value of the 5th element? does cout<<a[5]; will do? And what if i want to output all the elements? do I write cout<<a[0], a[1], a[2]... a[20]; Sorry I just cant totally understand the use of array.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: Array

 
0
  #7
Aug 10th, 2005
>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:
  1. for (int i; i < 20; i++)
  2. {
  3. cout << a[i] << '\n';
  4. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array

 
0
  #8
Aug 10th, 2005
#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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

Re: sort Array

 
0
  #9
Apr 27th, 2008
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.

  1. #include <iostream>
  2.  
  3. void display(int arr[], int size);
  4.  
  5. int main()
  6.  
  7. {
  8. using namespace std;
  9. //int arr[13] = {80, 127, 120, 42, 115, 125, 120, 49, 126, 42, 115, 126, 73};
  10. const int size = 13;
  11. int arr[size] = {80, 127, 120, 42, 115, 125, 120, 49, 126, 42, 115, 126, 73};
  12. sort(arr, size);
  13. cout << "Display numbers in the array" << endl;
  14. for(int i=0; i < size; i++)
  15. cout << " Integer [arr]: " << arr[i] << endl;
  16.  
  17. //Calculate the average value
  18. double total = 0.0;
  19. for(int i = 0; i < 13; i++)
  20. total += arr[i];
  21. double ave = total / 13;
  22.  
  23. //report the results
  24. cout << " Average value = " << ave << endl;
  25.  
  26. //Print numbers above the average
  27. cout << "Display number above average" << endl;
  28. for(int i=0; i < 13; i++)
  29. if(arr[i] > ave)
  30. cout << " Integer [arr]: " << arr[i] << endl;
  31. //Print numbers below the average
  32. cout << "Display number below average" << endl;
  33. for(int i=0; i < 13; i++)
  34. if(arr[i] < ave)
  35. cout << " Integer [arr]: " << arr[i] << endl;
  36. cout << "Display characters for the array" << endl;
  37. for(int i=0; i < 13; i++)
  38. cout << (char)(arr[i]-10);
  39.  
  40.  
  41. cin.get();
  42. return 0;
  43. }

What did I do wrong???
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Array

 
0
  #10
Apr 27th, 2008
> What did I do wrong??
Not look at the post dates to realise that the person you're replying to hasn't been to this board in the last 2 years.
Post a new question, don't just dig up threads from the graveyard.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC