Question

In: Computer Science

C/ C++ Preferably 1. Write a simple program where you create an array of single byte...

C/ C++ Preferably

1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++) in others it's more difficult due to type casting restrictions (Java/Python). Make sure you can access both character and integer data at any location in the array.


In C/C++ think of the follow type of code:
int j, i, *ii;
char c, *cc;
char mem[MEM_SIZE];
ii = (int*)(mem + 10); //base address + 10
cc = mem+10;


2. Read data from a text file. Can't come up with your own test data, cut and paste the words to 'Mary had a little lamb' into a file or down load a text (.txt) version of the bible (big) from the internet.
3. Use regular expressions, strtok or any command you want including something you write yourself to break a line of text read from a file into tokens. A token is just a sequence of characters without a separator. The only separator you need is space ' ' but if you want to use more like, comma ',', tab '\t' and semicolon ';' go for it.

Solutions

Expert Solution

Answer 1

Code

#include <stdio.h>

#define MEM_SIZE 100

int main()
{
   int i, *ii;
   char *ch, c;
   char arr[MEM_SIZE] = { 0 };

   //writing value 243 in character array starting from location arr+10
   ii = (int*)(arr + 10);
   *ii = 243678782; // 3e 3e 86 0e will be stored at positions 10, 11, 12 and 13 respectively. LSB will be stored at starting position and so on

   // Retreiving value as character byte at location arr+10
   ch = (char*)(arr + 10);
   c = *ch;
   printf("Before changing value of integer starting position 10 is: %d\n", *ii);

   printf("value of c at position 10 is: %c\n", c); // c will get value as 3e which means character '>' (Ascii conversion)

   // Now change the value at location arr+10 and retrieve using integer
   *ch = '8';
   printf("Changed charater value at position 10 is: %c", *ch);
   printf("\nvalue of integer starting position 10 is: %d", *ii); // New value will become 38 3e 86 0e => 243,678,776‬. Position 10 byte is MSB.

   return 0;
}

Test Output


Related Solutions

Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the...
Write a program in c++ that reads x[4]. Then create the array y[6||6], such that the first quarter of y consists of the value of the first element of x. The second quarter of y consists of the value of the second element of x. The third quarter of y consists of the value of the third element of x. The fourth quarter of y consists of the value of the fourth element of x?
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT