In: Computer Science
Exercise 4: We will use the code in ArrayDemo.cpp to explore arrays and the relationships between arrays and pointers. First of all, make sure you understand the purpose and syntax of the forward declarations at the beginning of the file. Then do the following (90 minutes in coding):
------------------------------------------------------------------------------------------------------------------------------------
ArrayDemo.cpp (below)
#include <iostream>
using namespace std;
// Forward declarations.
void display(int data[], int size);
void bubble(int data[], int size);
void swap(int data[], int idx1, int idx2);
int main(int argc, const char * argv[])
{
const int SIZE = 7;
// These are auto variables.
int set[] = {1,4,3,2,5,9,8}; // Size implicit
int set2[] = {30, 23, 25, 19, 100, 12, 7};
// How does it look like in JAVA?
// int[] myArray = new int[3];
// int[] myArray = {1, 2, 3};
// int[] myArray = new int[] {1, 2, 3};
// You have to pass the size in; a C++ array is just a dumb block of
// storage; no size information is carried with it and no bounds
// checking is done.
display(set, SIZE);
bubble(set, SIZE);
display(set, SIZE);
return 0;
}
void display(int data[], int size) {
for(int i = 0; i < size; i++) {
cout << data[i] << ",";
}
cout << std::endl;
}
void bubble(int data[], int size) {
for(int i = 0; i < size; i++) {
for(int k = 0; k < size - 1 - i; k++) {
if(data[k] < data[k+1]) {
swap(data, k, k+1);
}
}
}
}
void swap(int data[], int idx1, int idx2) {
int temp = data[idx1];
data[idx1] = data[idx2];
data[idx2] = temp;
}
//Forward Declaration in the above program are function prototypes. These are used to define return type and data type of parameters passed to the function.
For part 3 of the question :- Memory access through pointers is said to be more efficient than memory access through an array ie the second method because it reduces the storage space and complexity of the program.
Setting all pointer variables in main() to null after deleting storage is a precautionary step so that our pointer does not points to any garbage value .This makes the program much safer and cleaner.
CODE SNIPPET: ( Check the //comments for better understanding of each question)
#include <iostream>
using namespace std;
// Forward declarations.
void display(int data[], int size);
void bubble(int* pointer, int size);
void swap( int *idx1, int *idx2);
int LinerSearch(int data[], int size,int target);
int main(int argc, const char * argv[])
{
const int SIZE = 7;
int *set = new int[SIZE] {1,4,3,2,5,9,8}; //part 4 of question: declaring arrays set and set2 dynamically using "new"
int *set2 = new int[SIZE] {30, 23, 25, 19, 100, 12, 7};
display(set, SIZE);
bubble(set, SIZE);
display(set, SIZE);
int SearchIndex=LinerSearch(set2,SIZE,25); //part 1 of qs :
testing LinerSearch
cout<<SearchIndex;
delete[] set;
delete[] set2;
set=NULL;
set2=NULL;
return 0;
}
void display(int *data, int size) {
for(int i = 0; i < size; i++) {
cout << *(data+i) << ",";
}
cout << std::endl;
}
void bubble(int* pointer, int size) // Part 3 of question:
bubble() so that it uses a pointer and pointer arithmetic
{
for(int i = 0; i < size; i++) {
for(int k = 0; k < size - 1 - i; k++) {
if(*(pointer+k) < *(pointer+k+1)) {
swap(pointer+k,pointer+k+1);
}
}
}
}
void swap( int *idx1, int *idx2) { //part2 of qs: swap() so that it
takes two pointers
int temp = *idx1;
*idx1 = *idx2;
*idx2 = temp;
}
int LinerSearch(int *data, int size,int target) //part1 of the
question :ADDING LinerSearch()
{
for(int i=0;i<size;i++)
{
if(*(data+i)==target)
return(i);
}
return(-1);
}
SCREENSHOT OF MY IDE
SCREEN SHOT OF OUTPUT
*2 on the last line of output is the index printed after calling LinearSearch().