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 :)

Recommended Answers

All 12 Replies

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.

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.

//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;
}

<< 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.

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.

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.

>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:

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
*/

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.

#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???

> 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.

I see, I’m new to this site and thought this person may still hook. I turned in this assignment and instructor was complained that I did not do what assignment required, I suppose to use the sort function to sort int arr and display it before and after sort. I was searching this site to see how other people used the sort function, and then I found this thread, which is similar to what I want to do. I used the sort function but did not compile have error.
[error]
error C3861: 'sort': identifier not found
[error]

Even if it compile pass the sort function point, it seem that the code is missing something.

Hello mitrmkar,

Thanks for the pointer; I never thought that such info out there. I mod the code and it work, except the last part did work right, it suppose to display [display] Fun isn't it? [/display] but it display

?Fiinnsttu

, and how can I get around without using the #include <algorithm> instructor might not like it if I use it

#include <iostream>
#include <algorithm>

int main()

{
			using namespace std;
			int arr_size = 13;
			int arr[] = {80, 127, 120, 42, 115, 125, 120, 49, 126, 42, 115, 126, 73};
			cout << "Display numbers in the array before sort" << endl;
					for(int i=0; i < arr_size; i++)
			cout << " Integer [arr]: " << arr[i] << endl;
			cout << endl;
			
					sort(arr, arr + arr_size);
  			cout << "Display numbers in the array after sort" << endl;
			for(int i=0; i < arr_size; i++)
			cout << " Integer [arr]: " << arr[i] << endl;

			//Calculate the average value
			double total = 0.0;
			for(int i = 0; i < arr_size; i++)
				total += arr[i];
			double ave = total / arr_size;

			//report the results
			cout << " Average value = " << ave << endl;

			//Display numbers above the average
			cout << "Display number above average" << endl;
			for(int i=0; i < arr_size; i++)
				if(arr[i] > ave)
					cout << " Integer [arr]: " << arr[i] << endl;
			//Display numbers below the average
			cout << "Display number below average" << endl;
			for(int i=0; i < arr_size; i++)
				if(arr[i] < ave)
				cout << " Integer [arr]: " << arr[i] << endl;
			cout << "Display characters for the array" << endl;
			for(int i=0; i < arr_size; i++)
				cout << (char)(arr[i]-10);
						

cin.get();
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.