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

In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
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...
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you...
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you may break it down into methods, but that is NOT         required.]    a.     Set up 4 arrays which each hold 6 employee’s data:              int[ ] empid              int[ ] hours              double[ ] rate              double[ ] wages          b.     Set up loops to load the empid, hours and rate arrays    c.     Set up a loop to calculate values for the wages array. TAKE OVERTIME [hours > 40],  INTO CONSIDERATION, thru the use  of...
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...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
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...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT