Question

write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

write C code that uses pointers, arrays, and C strings.

3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below.

int pow_xy(int *xptr, int y);

Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow.

For the remaining problems, use array syntax if a parameter in a function’s prototype is specified as an array (using [ ]), and pointer syntax if a parameter is specified as a pointer (no [ ] allowed).

4. Write a function called init_array. It is passed 3 parameters, as specified in the prototype below:

int init_array(int array[], int len, int start_value);

The ith item in array should be initialized to the value start_value * i. Also, the function should return the sum of the values in array after initialization.

5. Write a function called reverse_numbers. Here is its prototype:

void reverse_numbers(int *arrayptr, int len);

The function is passed a pointer to the beginning of an array of ints (and of course the length of the array as a 2nd parameter). The function should reverse the ordering of the numbers in the array.

For the remaining problems, you may not use any functions in .<string.h>

6. Write a function called streql. Here is its prototype:

int streql(char *strptr1, char *strptr2);

It is passed two parameters, both of which are pointers to C strings. It returns a non-zero integer if the strings consist of the same sequence of characters (with a ‘\0’ at the end), or 0 otherwise.

Here are some examples of how your code should work:

streql("bin", "bag"); // returns 0

streql("computer", "game"); // returns 0

streql("computer", "computer"); // returns a non-zero

int streql("are", "area"); // returns 0

7. Write a function called strcmp373. Here is its prototype:

int strcmp373(char str1[], char str2[]);

It is passed two parameters, both of which are C strings. It returns 0 if the strings are the same alphabetically, or a negative integer if str1 is alphabetically before str2, or a positive integer otherwise.

Here are some examples of how your code should work:

strcmp373("bin", "bag"); // returns s positive int

strcmp373("computer", "game"); // returns a negative int

strcmp373("computer", "computer"); // returns 0

strcmp373("are", "area"); // returns a negative int

8. Write a function called strcat373. The function is passed two parameters, both of which are pointers to C strings. Here is its prototype:

char *strcat373(char *destptr, char *srcptr);

The function should modify the first string (array) so that contains the concatenation of the two strings. For example:

int main() {

char str1[9] = "comp";

char str2[ ] = "uter";

printf("str1 contains %s\n, str1); // prints computer

Note that strcat373 is guaranteed to work properly only if str1's length is big enough to contain the concatenation of the two strings. In this case, "computer" takes up 9 bytes, and since str1 is 9 bytes, the function should run properly since the array is just long enough to contain “computer” (plus ‘\0’). On the other hand,

:

char str1[] = "comp"; // only 5 bytes

char str2[] = "uter";

strcat373(str1, str2); // takes up 9 bytes and

// therefore overflows str1

Upon execution, a runtime error may occur, or (even worse) no runtime error will occur, but some other variable(s) in your program may be overwritten.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

***Please upvote if you liked the answer***

Screenshot of the functions and a main function for demonstration:-

#include <stdio.h> int pow_xy (int *xptr, int y) int i=0; //To store the result as the new value of x int val = 1; for (i = 0

Screenshot of the output:-

5 4 3 2 1 11 1 9 -1 stri contains computer Process returned 22 (0x16) Press any key to continue. execution time : 0.098 s

C code to copy:-

#include <stdio.h>

int pow_xy(int *xptr,int y)
{
int i=0;

//To store the result as the new value of x
int val = 1;
for(i = 0;i<y;i++)
{
val = val * *xptr;
}
//To Return the result
return val;
}

int init_array(int array[], int len, int start_value)
{
int i=0,sum = 0;

for(i = 0;i<len;i++)
{
//The ith item in array should be initialized to the value start_value * i
array[i] = start_value * i;
sum = sum + (start_value * i);
}
//Returning the sum of the values in array after initialization
return sum;
}

void reverse_numbers(int *arrayptr, int len)
{
int i,j,temp;
for(i = len - 1,j = 0;i>=0;i--)
{
//Reversing operation
if (i != j && i > j)
{
temp = arrayptr[i];
arrayptr[i] = arrayptr[j];
arrayptr[j] = temp;
}
//If the array reaches or crosses the midpoint,stop iteration
else if(j >= i)
{
break;
}

j++;
}
}

int streql(char *strptr1, char *strptr2)
{
int result;

while(*strptr1 != '\0' || *strptr2 != '\0')
{
if(*strptr1++ == *strptr2++)
{
result = 1;
}
//While encountering non-matching characters
else
{
result = 0;
break;
}
}

//If the strings are of unequal size
if(*strptr2 != '\0')
result = 0;

return result;
}

int strcmp373(char str1[], char str2[])
{
int i = 0;

int result;

//Looping till one of the strings ends
while(str1[i] != '\0' || str2[i] != '\0')
{
//If any character in the first string is found alphabetically after
//the corresponding character in the second string
if(str1[i] > str2[i])
{
result = 1;
break;
}

//If any character in the first string is found alphabetically before
//the corresponding character in the second string
else if(str1[i] < str2[i])
{
result = -1;
break;
}
//If any character in the first string is found alphabetically same as
//the corresponding character in the second string
else
result = 0;


i++;

}
return result;
}

char *strcat373(char *destptr, char *srcptr)
{
//Moving to the end of the destination string
while(*destptr != '\0')
{
*destptr++;
}
//Looping till the end of the second string is reached
while(*srcptr != '\0')
{
//Appending each character from the second string to the first string
*destptr++ = *srcptr++;
}
}

//Main method for testing purposes
void main(){
int x = 2,y = 3,i;
int arr[6];

char str1[9] = "comp";
char str2[] = "uter";

//Testing pow_xy
printf("%d\n",pow_xy(&x,y));
//Testing init_array
printf("%d\n",init_array(arr,6,1));

reverse_numbers(&arr[0],6);

//Testing if the array is reversed
for(i = 0;i<6;i++)
{
printf("%d ",arr[i]);
}
//Testing the streql
printf("\n%d\n",streql("bin", "bag"));
printf("%d\n",streql("computer", "computer"));
printf("%d\n",streql("are", "area"));

//Testing the strcmp373
printf("\n%d\n",strcmp373("bin", "bag"));
printf("%d\n",strcmp373("computer", "game"));
printf("%d\n",strcmp373("computer", "computer"));
printf("%d\n",strcmp373("are", "area"));

//Testing the strcat373
strcat373(&str1[0],&str2[0]);

printf("str1 contains %s",str1);
}


Add a comment
Know the answer?
Add Answer to:
write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....
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
  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • Write a function that takes an array of C-strings as a parameter and the number of...

    Write a function that takes an array of C-strings as a parameter and the number of elements currently stored in the array, and returns a single C-string pointer that is the concatenation of all C-strings in the array. Note: The function must take 2 parameters and return "char *". Any other ways will cause some deduction of points.

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr);...

    C program help 4. Write a function called scan_hex. Here is its prototype: void scan_hex(int *xptr); Here is an example of its use, in conjunction with print_decimal. Assume that the user will type a non-negative hex number as input. Also, assume that the “digits” a-f are in lower case. Let’s say the user types 1b int x; scan_hex(&x); print_decimal(x); MODIFY THIS CODE // function inputs a non-negative integer and stores it into *xptr. // The format of the input is...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

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