Question

In: Computer Science

1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2)...

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.

Solutions

Expert Solution

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


Related Solutions

Define parallel arrays, tell how the data in each array is accessed and give an example...
Define parallel arrays, tell how the data in each array is accessed and give an example of parallel arrays.
1.Give an advantage and a disadvantage of a parallel combination. Give an example. 2.Give a few...
1.Give an advantage and a disadvantage of a parallel combination. Give an example. 2.Give a few essential features of an ordinary cell. 3.How should two identical batteries be connected in order to obtain the maximum EMF from the combination, should it be in series or parallel? Why? ? 4.How does the resistance of a conductor affected by its length and diameter?
1. Give and explain 3 example of financial instrument 2. Give and explain 3 example of...
1. Give and explain 3 example of financial instrument 2. Give and explain 3 example of Generalized Audit Software 3. Why it called Generalized Audit Software? 4. Give 1 of the most famous GAS
Read and print parallel array - How can this be made to read parallel arrays and...
Read and print parallel array - How can this be made to read parallel arrays and then print them? The program presented here is intended to read from the text file and build parallel arrays. Then it will print as shown. The first line will not be stored in the array. The second line will be stored in firstArray[] and the third line will then be stored in secondArray[] and the arrays will repeat until the file is read. begin...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
1. What is inflation. Give an example. 2. What is hyperinflation. Give an example. 3. Explain...
1. What is inflation. Give an example. 2. What is hyperinflation. Give an example. 3. Explain the causes of hyperinflation? 4. Explain the costs of hyperinflation. 5. Why sometimes governments are unable to borrow by issuing debt?
1- How IMAF's would work to dissolve something? 2- give me one IMAF example
1- How IMAF's would work to dissolve something? 2- give me one IMAF example
Give an example of integers a, b, m such that a 2 ≡ b 2 (mod...
Give an example of integers a, b, m such that a 2 ≡ b 2 (mod m), but a 6≡ b (mod m)
1. A) Explain the meaning of the term internal controls. B) Give an example of an...
1. A) Explain the meaning of the term internal controls. B) Give an example of an internal control. C) Explain the control objectives that internal controls achieve. D) Describe the types of internal controls. 2. Discuss the internal control regulatory environment – which laws or organizations require that companies maintain a system of internal controls.
1. Explain prospect theory. Give 1 real-life example to illustrate how the theory works. 2. Explain...
1. Explain prospect theory. Give 1 real-life example to illustrate how the theory works. 2. Explain how organizations conduct capability analysis when developing a new product. Give 1 example to illustrate your answer. 3. Discuss the customer adoption model of new products. Give 1 example to illustrate your answer. 4. Discuss the design spectrum; give an example to illustrate your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT