Write declaration of a C++ class that might be used to implement a binary tree with each
Question:
Write declaration of a C++ class that might be used to implement a binary tree with each node able to hold an integer. Your implementation (i.e. the class itself and those bodies which conveniently fit within it) should make it impossible for casual programmers to access the pointer fields that link parts of the tree together except through cleanly specified access functions. Show how you would overload the "+" operator in C++ to provide a neat notation for adding a new item into such a tree. [20 marks]
A slightly clumsy programmer had been lagging behind the company productivity targets and needed to write some C++ code in a hurry. Almost remembering an old optimising compilers question from student days, this programmer produced a file containing the text: struct List { int head; struct list *tail; }; struct List readlist() { int i; struct List *p, *q, *t; L1: p = NULL; L2: while (scanf("%d", i) = 1) /* scanf reads an integer and returns 1 if it finds one correctly /* [ L3: t = malloc(sizeof(List *)); if (t == 0) printf("oops no memery "); else t->hd = i; L4: t->tl = 0; if (p == NULL) p = q = t; else q->tl = t, q = t; } L5: return p; } Unfortunately the programmer had forgotten what this was supposed to achieve; you are asked to help re-create an explanation for the code and to identify problems (of either style or correctness) in it. You do not need to provide a correct version of the program: just draw attention to as many errors or oddities as you can. Suggest two ways in which a move from C to C++ might allow the structure of the code to be improved. [20 marks
For five of the following C or C++ features write very short fragment of code (perhaps 2 or 3 lines will suffice in most cases) that illustrates the syntax involved. In each case explain very briefly what your example achieves. (a) preprocessor macros and conditional compilation (b) casts that convert from one pointer type to another (c) C and C++ style comments (d) the declaration of a simple C++ class (e) overloading the operator '+' (f ) the C setjmp function (g) the switch statement, including a default label
i need help for this project (if You are expected to investigate a case study (either drawn from your work site, or alternatively being secured through formal access) to analysis only two business processes from commencement to completion, with a focus on the more important or interesting tasks within the process. Be careful to select business processes of appropriate size. If you choose the processes which are too simple, you will find it hard to demonstrate your mastery of MIS concepts. Conversely, if you choose processes that are too complex, you may find it difficult to complete the assignment within the specified time. Consider discussing your choice with me before you begin work. The central premise of this project is that you are able to develop familiarity with describing the functional activities of the organization of your chosen case study, investigating the functional problems, and suggesting remedial actions in view of the IS and its capabilities to improve outdated business process. should be focus may be based on the following:
Providing background information of your chosen organization/system. Identify the kind of systems (software). Describing TWO business processes; including all related business functions -Please draw on wide sets of process mapping tools, i.e. a cross-functional diagram or alternatively flowchart. Therein, for rigors and coherence purposes, you will need to draw on relevant literature and modelling techniques to the issues raised in your chosen case study. Describing the technology and its main IT infrastructure components in your chosen case. Outlining organizational benefit from the extant IS application in the chosen process. Articulating the management level involved in using the system. Discussing the business and IS strategies influenced by this kind of technology. Commenting on any issues dysfunctional areas that arose during research of the case study. it should be for example like SAP or oracle for example in any organization for example in HR what is the process with the follow diagram
I'm having a hard time figuring out this assignment. Thank you for any assistance.
(Name this program StuPreTestNumFileArr) Revise the TestNumFile program from lecture Unit3 File Input and save the work in a new program TestNumFileArr. Practice sorting and insertion.
Step 0. First set up the given code in the new program and make sure it works properly.
Step 1. Add a private helper method:
// return a partially filled int array with all elements from ArrayList
// param in the same sequence and 5 extra spots at the end
private static int[] toIntArray(ArrayList list) {
// ADD code
}
The idea is to copy the data from the ArrayList to an array. To allow space for additional operations, make this array to be a partially filled array. Accomplish this by creating an array whose capacity is 5 + list.size().
Step 2. Add the following code to the end of main():
int[] numArr = null; // to hold data
int size = 0; // track actual # of elements.
numArr = toIntArray(numList);
size = numList.size();
System.out.println("Now in arr: " + Arrays.toString(numArr));
// need import
This segment of code should print the same data plus 5 zeros at the end:
Now in arr: [15, 6, 74, 28, 90, 34, 0, 0, 0, 0, 0]
Step 3. Add a private helper method to sort this partially filled array following the given method prolog comment. You must writown sorting code and use selection sort or insertion sort.
// sort a partially filled array ([0 ~ numOfElements-1]) into
// ascending order
// Will return and not modify the arr if arr is null or
// numOfElements is invalid
private static void sort(int[] arr, int numOfElements) {
// ADD code
}
numOfElements is invalid if it's negative or larger than arr.length.
Step 4. Add the following code to main() to test the sorting method (continue from step 2 work):
sort(numArr, size);
System.out.println("After sorting: " + Arrays.toString(numArr));
This segment of code should print the data after sorting. Be aware that the 5 zeros at the end should not be touched by the sort() method.
After sorting: [6, 15, 28, 34, 74, 90, 0, 0, 0, 0, 0]
Step 5. Add a private helper method to insert a new item into the partially filled array. Do not add the new item to the end of the filled area and then sort. Insert and maintain the sorted order.
// insert into a partially filled array ([0~numOfElements-1]) and
// maintain sorted order (ascending)
// This method returns number of stored elements at the end.
// If insert failed (such as arr is null, numOfElements is invalid,
// or arr is already full), do not modify arr content and return
// numOfElements
private static int insert(int[] arr, int numOfElements, int newItem) {
// ADD code
}
Step 6. Add the following code to main() to test the insert method (continue from step 4 work):
Scanner stdIn = new Scanner(System.in);
int num;
for (int i=0; i<2; i++) {
System.out.print("Enter a number: ");
num = stdIn.nextInt();
size = insert(numArr, size, num);
System.out.printf("After inserting %d: %s ", num, Arrays.toString(numArr));
}
stdIn.close(); // Eclipse requires closing a Scanner object
This segment of code will generate this output with two inputs -5 and 60:
Enter a number: -5
After inserting -5: [-5, 6, 15, 28, 34, 74, 90, 0, 0, 0, 0]
Enter a number: 60
After inserting 60: [-5, 6, 15, 28, 34, 60, 74, 90, 0, 0, 0]
*****************************
This is the TextNumFile:
// classes used for file i/o and i/o exception import java.io.IOException; import java.nio.file.Path;
import java.util.Scanner; // I/O methods
import java.util.ArrayList;
public class TestNumFile {
// call method to load data from file and prepare for processing public static void main(String[] args) { ArrayList numList = null; // declare an ArrayList var and initialize to null
// relative path and file name of data file String fileName = "data/records.txt"; // This "records.txt" file should already exist in a folder named "data". // If using an IDE and source code file is put in a default "src" folder (or similar), // folder "data" should be at the same location as the "src" folder; // otherwise, "data" folder should be at the same location as the .java file
numList = loadFromFile(fileName); // load data
System.out.println("Records: " + numList); // ... more processing ...
} // end main
// read data from a specified file // data will be saved in ArrayList and returned private static ArrayList loadFromFile(String fileName) { Scanner fileIn = null; // scanner object to connect to file ArrayList list = new ArrayList<>(); // to store data from file
try { // try-catch-finally used for any exception during file open/read/close // open input file fileIn = new Scanner( Path.of(fileName) );
// loop through multiple records while (fileIn.hasNextInt()) { // still have numbers to be read? // 1. read one record (here each record is just one num) int num = fileIn.nextInt();
// 2. add the record to ArrayList obj list.add(num);
// end one record }// end while: reading all records
} catch (IOException ioe) { System.out.println("Error reading "" + fileName + "" file: " + ioe); } finally { // close file if ( fileIn != null) { // close if was connected to a file fileIn.close(); } } // end file input
return list; }// end loadFromFile
} // end class TestNumFile
****************************
This is the data/records.txt file
15 6 74 28 90 34
(a) Find at least 2 sources of undefined behaviour in the following program, and write acorrected version of this function. [5 marks] int main(void) { char *s = "abcde"; int len = strlen(s); for (int i = 0; i <= len; i++) s[i] += 1; return printf("'%s' is %d characters long ", ++s, strlen(s)); } (b) Restructure the program below to be more cache-efficient, giving the code and explaining your changes. [5 marks] typedef struct point { double x, y, z; } Point; int find_max_x_argument(int n, Point *elems) { double max = 0; int max_index = 0; for (int i = 0; i < n; i++) if (max < elems[i].x) { max_index = i; max = elems[i].x; } return max_index; } (c) The following definition forms part of a legal C++ program: int foo() { MyClass x(1,2); MyClass y = C(3,4); MyClass z = x; MyClass t; z = x; z.f = x.f; return z.f; } (i) Give a declaration of MyClass which enables foo to compile and run, noting any methods or constructors in MyClass which are invoked when foo is called. [Note: Precise C++ syntax is not necessary to obtain full marks.] [4 marks] (ii) Having seen your declaration of MyClass, a colleague points out some of the lines of foo may be redundant. Which are these? [2 marks] (iii) Your boss now replaces your declaration of MyClass. Not having access to the new declaration, explain, giving reasons, which if any lines of foo are now redundant.
(d) Giving a suitable example, explain the effect in C++ of qualifying a member function (method) with virtual. [3 marks] (e) Recode the following Java code in C++. Minor syntactic errors will not be penalised. class Foo { final int[] v; final int s; public Foo(int n) { s = n; v = new int[n]; } // In Java garbage collection de-allocates arrays // appearing in no-longer-used instances of Foo. // In C++ an alternative solution is required.
(a) (i) In programming languages in general, what is the difference between statically and dynamically-allocated variables? Without using the static keyword, give examples of both types of variable in the C language. [2 marks] (ii) What effect do the static and extern keywords have in C for both forms of variable? [3 marks] (b) There are 32 characters in the ASCII set between 'A' and 'a'. All are printable. A potentially-infinite stream of 8-bit bytes can be made printable by encoding into base 32 and represented by a longer stream just using these characters. Give a syntactically-accurate definition in C for encode32(unsigned char a), called for each byte, which invokes putchar(char c) once or twice per call to render the encoded output. [5 marks] (c) Show how the for statement in C can be used to traverse a linked list. Explain the benefits of this coding style, mentioning whether the continue statement works appropriately. [4 marks] (d) typedef char * mystring; mystring s201 = "201"; mystring s202 = s201; s202[2] += 1; A novice programmer writes the above code. What you think they are intending to do and what two problems might they suffer? [2 marks] (e) An interpreter for a string processing language is written in C. Describe four storage and efficiency-related considerations when designing the module for storing strings for the interpreter? [4 marks
(a) A key/value store holds pairs of strings in a non-volatile memory (NVM). The NVM is mapped as a memory region between two hardcoded virtual addresses, NV START and NV END. This memory is all set to zero as manufactured and can be freely read, but any write operations result in a logical ORing of the new data with the old data at the addressed location. Bits can never be cleared. Strings representing keys and values will be fewer than 250 characters long. Eventually the memory will fill up, but sufficient is available for the intended application. The API provides the following two operations: int store(char *key, char *value) // returns non-zero on error, and char *lookup(char *key) // returns NULL if not found. Provide a full C implementation of the store routine, explaining how the lookup function and changes to values under a given key would work, if this is not obvious from your code. Code efficiency is not important. Hint: You can directly access the non-volatile store using a construct such as ((char *)NV_START)[offset]. [10 marks] (b) The following text almost constitutes both a C++ and Java program: class Foo { public: int x, y; }; class Test { private: void f1(Foo p) { p.x = 1; } private: void f2(Foo &q) { q.y = 2; } //[Java]: ignore '&' public: void test() { Foo p; //[Java]: replace with: 'Foo p = new Foo();' p.x = p.y = 99; f1(p); Foo q = p; p = q; f2(q); print("HERE"); } }; (i) Explain the storage structure accessible from variables x and y when control reaches print("HERE") following a call to test(), both in the Java and the C++ interpretations. [3 marks] (ii) For the C++ interpretation, show how adjustments only to the definition of class Foo can cause (useful) debugging-style output to be produced at as many places as possible during the execution of method test(). [7 mark
Convert the below C code to basic MIPS. Please leave comments for explanation #include int main(void) printf("lnsert two numbers "); int a,b scanf("%d", &a); scanf%d,ab)}; a=a<<2 b=b<<2; printf%d&%d ",a, b); return 0
kindlly answer all
Principles of Information Systems
ISBN: 978-0324665284
9th edition
Authors: Ralph M. Stair, George W. Reynolds