In: Computer Science
1)
a) Give short code example of parallel arrays.
b) Explain how parallel arrays work.
2)
a) How would you compare two arrays to check if they have the same values?
b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing?
3
a) Can you encounter memory violation using an array?
b) If yes explain. If no, explain why not.
1)
a) Give short code example of parallel arrays.
#include <iostream> #include <string> using namespace std; int main() { int max = 0, index = 0; string empName [ ] = {"Harry", "Sally", "Mark", "Frank", "Judy" }; string empDept [ ] = {"IT", "Sales", "IT", "HR", "Sales"}; int empSal[ ] = {10000, 5000, 20000, 12000, 5000 }; int n = sizeof(empSal)/sizeof(empSal[0]); for(int i = 0; i < n; i++) { if (empSal[i] > max) { max = empSal[i]; index = i; } } cout << "The highest salary is "<< max <<" and is earned by "<<empName[index]<<" belonging to "<<empDept[index]<<" department"; return 0; }
b) Explain how parallel arrays work.
In computing, a group of parallel arrays (also known as structure of arrays or SoA) is a form of implicit data structure that uses multiple arrays to represent a singular array of records. It keeps a separate, homogeneous data array for each field of the record, each having the same number of elements.Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory .For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
2)
a) How would you compare two arrays to check if they have the same values?
#include <bits/stdc++.h>
using namespace std;
bool areEqual(int arr1[], int arr2[], int n, int m)
{
if (n != m)
return false;
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return
false;
return true;
}
// Driver Code
int main()
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int n = sizeof(arr1) / sizeof(int);
int m = sizeof(arr2) / sizeof(int);
if (areEqual(arr1, arr2, n, m))
cout << "Yes";
else
cout << "No";
return 0;
}
b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing?
ANS :- The if statement will compare the address of first element of array1 and array2 respectively.
In above code you can see the cout of a_1 and a_2 gives the address, which is the address of first element of both arrays respectively.
3
a) Can you encounter memory violation using an array?
ANS YES
b) If yes explain. If no, explain why not.
First we understand what causes memory access violation Memory access violation is most often caused by such errors in programs as array overruns or usage of a null pointer.A segmentation fault (segfault in abbreviated form) is a software error occurring when a program tries to access memory addresses unavailable for writing or when a program tries to modify memory using an illegal method.Segmentation is one of the approaches to memory management and protection in an operating system. In most systems it has been replaced by paged memory, but documentations traditionally use the term "Segmentation fault".
OR
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system).
What Memory Violation is....
Computer programs access system memory for processing. ... A memory access violation, also called a segmentation fault (or segfault), occurs when the program tries to access a memory location that doesn't exist, or is otherwise inaccessible. We call this trying to access an illegal memory location.
Hope you get the answer