Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//In this assignment, we wirte code to sum up the values of the first n bytes in a memory block //that is allocated for n

//In this assignment, we wirte code to sum up the values of the first n bytes in a memory block //that is allocated for n unsigned integers. //The memory allocation occurs in the main function //We need to implement the function sum_n_bytes //This function returns the sum of the values of the first n bytes that is pointed by //the pointer p

#include #include

//Implement the following function //The function sum_n_bytes returns the sum of the values of the first n bytes in the memory block//pointed by the pointer p unsigned sum_n_bytes(unsigned *p, int n) { //fill in your code below //Note that the sum should be an unsigned integer //This should has some ramifications on your code //Only a few lines of code expected

}

//Note how we use the command line arguments int main(int argc, char *argv[]) { if(argc != 2) { printf("Usage: %s n ", argv[0]); exit(-1); } int n = atoi(argv[1]); //allocate memory for n unsigned integers unsigned *p = calloc(n, sizeof(unsigned)); //Assign values to these unsigned integers in a certain pattern int i; for(i=0; i< n; i++) { p[i] = 1 << (i % 32); } //print out the sum of the first n bytes printf("%d ", sum_n_bytes(p, n)); //do not forget to free p free(p); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Factor the trinomial, if possible. 30a 2 + am - m 2

Answered: 1 week ago