Question

Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and...

Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and use memcpy and memmove to move the data around.

I suggest you do part 3a, test and verify it’s working as expected, then add part b, test & verify, then do part 3c.  You only need to turn in the complete program, but doing it in steps will help both with understanding and if you have bugs along the way.

Part 3a.

a.Allocate 2 char buffers of 128 bytes each using .  Call your pointers and (in memory of Dr. Seuss, of course!)

b.Initialize all 128 bytes of thing1 to asterisks (‘*’) using .

c.Set the 21 to a null char (‘\0’).  This will make the buffer contain a c-string of 20 asterisks (and terminated by the null char).

d.Use to set bytes 22-25 to percent signs.

e.Print  the console using .  You should get only a string of 20 asterisks.

f.Set the 42nd byte to a null char, and then
     
You should get your percent signs, followed by some asterisks, because you created a 2nd c-string starting at byte 22.

g.Print the numeric values of the first 50  will show you that your array contains 20 asterisks (42), a null char (0)  flagging the end of the c-string, 4 percent signs (37) and more asterisks.

Part 3b.

a. Set all 128 bytes of the 2nd buffer () to dollar signs, and set the 16th byte to a null char, to create a 15-char c-string of dollar signs.

b.Print the c-string out.

c.Use to copy the first 5 bytes of into bytes 8-12 of .

a.Remember that  and  parameters are

b.i.e., destination is 1st parameter, source is 2nd parameter.

d.Print again… if all goes well you’ll get some asterisks in the middle of your dollar signs.

Part 3c.  use memmove to shift the data around in thing1 and thing2 :

a. Use to move bytes 15-25 of 8 bytes to the left, then print out the  in .  If all goes well, you should end up with a shorter c-string, because it moved the bytes with the c-string- null-char over.

b.Print the numeric values of the first 50 bytes again to see if it did indeed move the bytes over for you.

c.Use to move the first 20 bytes of thing2 10 chars to the right, and print out the resulting thing2.

d.Print the numeric values of the 1st 50 chars of thing2 to see if was smart enough to move the data without overwriting.

a.(if you used  instead of , what would have happened?)

e.Did you get what you expected?

Name your program memmove_memcpy_memset.cpp

----------------OUTPUT IS PASTED BELOW TO CHECK--------------------------

Here's my Part 3 output:

part 3a

thing1: '********************'

thing1 + 22: '%%%%****************'

bytes starting at thing1

byte hex   int char

  0 0x2a 42 *

  1 0x2a 42 *

  2 0x2a 42 *

  3 0x2a 42 *

  4 0x2a 42 *

  5 0x2a 42 *

  6 0x2a 42 *

  7 0x2a 42 *

  8 0x2a 42 *

  9 0x2a 42 *

10 0x2a 42 *

11 0x2a 42 *

12 0x2a 42 *

13 0x2a 42 *

14 0x2a 42 *

15 0x2a 42 *

16 0x2a 42 *

17 0x2a 42 *

18 0x2a 42 *

19 0x2a 42 *

20 0x 0 0 .

21 0x2a 42 *

22 0x25 37 %

23 0x25 37 %

24 0x25 37 %

25 0x25 37 %

26 0x2a 42 *

27 0x2a 42 *

28 0x2a 42 *

29 0x2a 42 *

30 0x2a 42 *

31 0x2a 42 *

32 0x2a 42 *

33 0x2a 42 *

34 0x2a 42 *

35 0x2a 42 *

36 0x2a 42 *

37 0x2a 42 *

38 0x2a 42 *

39 0x2a 42 *

40 0x2a 42 *

41 0x2a 42 *

42 0x 0 0 .

43 0x2a 42 *

44 0x2a 42 *

45 0x2a 42 *

46 0x2a 42 *

47 0x2a 42 *

48 0x2a 42 *

49 0x2a 42 *

---------------------------------------------

part 3b

thing2 originally: '$$$$$$$$$$$$$$$'

thing2 w/ 5 chars of thing1: '$$$$$$$*****$$$'

---------------------------------------------

part 3c

thing1 before memmove: '********************'

thing1 after  memmove: '************'

bytes starting at thing1

byte hex   int char

  0 0x2a 42 *

  1 0x2a 42 *

  2 0x2a 42 *

  3 0x2a 42 *

  4 0x2a 42 *

  5 0x2a 42 *

  6 0x2a 42 *

  7 0x2a 42 *

  8 0x2a 42 *

  9 0x2a 42 *

10 0x2a 42 *

11 0x2a 42 *

12 0x 0 0 .

13 0x2a 42 *

14 0x25 37 %

15 0x25 37 %

16 0x25 37 %

17 0x2a 42 *

18 0x2a 42 *

19 0x2a 42 *

20 0x 0 0 .

21 0x2a 42 *

22 0x25 37 %

23 0x25 37 %

24 0x25 37 %

25 0x25 37 %

26 0x2a 42 *

27 0x2a 42 *

28 0x2a 42 *

29 0x2a 42 *

30 0x2a 42 *

31 0x2a 42 *

32 0x2a 42 *

33 0x2a 42 *

34 0x2a 42 *

35 0x2a 42 *

36 0x2a 42 *

37 0x2a 42 *

38 0x2a 42 *

39 0x2a 42 *

40 0x2a 42 *

41 0x2a 42 *

42 0x 0 0 .

43 0x2a 42 *

44 0x2a 42 *

45 0x2a 42 *

46 0x2a 42 *

47 0x2a 42 *

48 0x2a 42 *

49 0x2a 42 *

thing2 before memmove: '$$$$$$$*****$$$'

thing2 after  memmove: '$$$$$$$***$$$$$$$*****$$$'

bytes starting at thing1

byte hex   int char

  0 0x24 36 $

  1 0x24 36 $

  2 0x24 36 $

  3 0x24 36 $

  4 0x24 36 $

  5 0x24 36 $

  6 0x24 36 $

  7 0x2a 42 *

  8 0x2a 42 *

  9 0x2a 42 *

10 0x24 36 $

11 0x24 36 $

12 0x24 36 $

13 0x24 36 $

14 0x24 36 $

15 0x24 36 $

16 0x24 36 $

17 0x2a 42 *

18 0x2a 42 *

19 0x2a 42 *

20 0x2a 42 *

21 0x2a 42 *

22 0x24 36 $

23 0x24 36 $

24 0x24 36 $

25 0x 0 0 .

26 0x24 36 $

27 0x24 36 $

28 0x24 36 $

29 0x24 36 $

30 0x24 36 $

31 0x24 36 $

32 0x24 36 $

33 0x24 36 $

34 0x24 36 $

35 0x24 36 $

36 0x24 36 $

37 0x24 36 $

38 0x24 36 $

39 0x24 36 $

40 0x24 36 $

41 0x24 36 $

42 0x24 36 $

43 0x24 36 $

44 0x24 36 $

45 0x24 36 $

46 0x24 36 $

47 0x24 36 $

48 0x24 36 $

49 0x24 36 $

Program ended with exit code: 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
String [] wordList;
int wordCount = 0;
int occurrence = 1;
int arraySize = 100;
int arrayGrowth = 50;
wordList = new String[arraySize];
while ((strLine = br.readLine()) != null)   {
     // Store the content into an array
     Scanner s = new Scanner(strLine);
     while(s.hasNext()) {
         if (wordList.length == wordCount) {
              // expand list
              wordList = Arrays.copyOf(wordList, wordList.length + arrayGrowth);
         }
         wordList[wordCount] = s.next();
         wordCount++;
     } 
}
if (wordList.length == wordCount) {
    String[] temp = new String[wordList.length + arrayGrowth];
    System.arraycopy(wordList, 0, temp, 0, wordList.length);
    wordList = temp;
}
Add a comment
Know the answer?
Add Answer to:
Part 3.  Write a program to dynamically allocate some char buffers, use memset to fill them, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • == Programming Assignment == For this assignment you will write a program that controls a set...

    == Programming Assignment == For this assignment you will write a program that controls a set of rovers and sends them commands to navigate on the Martian surface where they take samples. Each rover performs several missions and each mission follows the same sequence: deploy, perform one or more moves and scans, then return to base and report the results. While on a mission each rover needs to remember the scan results, in the same order as they were taken,...

  • Need some assistance of reorganizing this whole program. I have the right code for everything I...

    Need some assistance of reorganizing this whole program. I have the right code for everything I just need help on putting all the codes in the right spot so it can come out to the correct output. output is supposed to look like this: 1 \\ user inputs choice to convert 12 to 24 8 \\ user inputs 8 for hours 30 \\ user inputs 30 for minutes 20 \\ user inputs 20 for seconds AM \\ user inputs AM...

  • Write a C++ program with a function to read in the file parts.txt into either parallel...

    Write a C++ program with a function to read in the file parts.txt into either parallel vectors or a vector of structs. The data file is on ANGEL in zipped format. The first few lines of the file look like: P-13725 A 23 61.46 P-13726 B 12 51.08 P-13754 D 27 4.56 P-13947 C 34 27.71 Representing part number, Class, On hand balance, cost. After the vector(s) has/have been filled, display a menu which allows the user to request the...

  • In problem 3, you will write a function, findvertically (), which will determine whether or not...

    In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a word search puzzle vertically. In word search puzzles, words can be written upwards or downwards. For example, "BEAK" appears at [1] [3] written downwards, while "BET" appears at [2] [2] written upwards: [["C", "A", "T", "X", "R"], ["D", "T", "E", "B", "L"], ["A" "R" "B", "E", "Z"], ["X", "O", "E", "A", "U"], ["J", "S", "O", "K", "W"]] Your...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • These are my answere to the following questions: are they right? 1. B 2. T 3....

    These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT