Question

I need its answer as soon as possible. Write a C program that uses malloc to...

I need its answer as soon as possible.

Write a C program that uses malloc to find the sum on a 1-D int array that holds 1000 integers, incriminating from 1. Also share the output and discuss what you have learned to complete the task.

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

#include <stdio.h>
#include <stdlib.h>

#define N 1000

int main()
{
int n , i , *ptr , sum = 0;

ptr = (int*) malloc(N * sizeof(int));
if(ptr == NULL)
{
printf("Memory not allocated");
exit(1);
}

for(i = 0 ; i < N; ++i)
{
   *(ptr + i) = i + 1;
sum += *(ptr + i);
}

printf("Sum = %d", sum);
free(ptr);
   return 0;
}

:- And we learn with help of this task ,how to use malloc. And also know importance of malloc.

Add a comment
Know the answer?
Add Answer to:
I need its answer as soon as possible. Write a C program that uses malloc to...
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
  • I need help with the C++ for the following problem: Write a program that can store...

    I need help with the C++ for the following problem: Write a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. 2. Output the content (value) that stored in the array of five elements. 3. Continue to ask the user to store for storing data into another array.

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • C PROGRAM: answer both or don’t answer please! Problem 0.1 Write a C program that uses...

    C PROGRAM: answer both or don’t answer please! Problem 0.1 Write a C program that uses a loop to print all perfect cubes (1, 8, 27,...) up to 432 Question 0.2 What is the output of the following code ? int Array/41/8-111,-1,2), 3,4,-5), ,-1,0. 2,3,-7) int P3 int i.j for (i-Oi<3; it) Ph Arrayli for (i-0; i < 3; it+) Alilo)- *Pij+3) for (i-;i<4; ) printf("%d%e" ,A[i [0],',');

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes...

    d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes as parame- ters any three integers and returns the largest of the three integers. Your output should have the same format and should work for any integers a user enters Desired output: Enter three integers: 6 15 8 The largest integer is: 15 7.3 Recursive Functions Write a program that uses a function sum(int) that takes as an argument a positive integer n and...

  • I need some with this program, please look at the changes I need closely. Its running...

    I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...

  • Coding in C. Please only use stdio.h (which would mean no malloc or anything like that)...

    Coding in C. Please only use stdio.h (which would mean no malloc or anything like that) (30 pts) Write a sorting program using an array. First read n integers and store them in an array. Then use any sorting algorithm (you can choose any one) to sort these numbers in ascending order. Turn in your code and input/result together. For example, if your input is 10 -9 5 1000 -9 1 20 -100 then your output should be -100 -9...

  • Please I need code in C++ Write a program that creates an int array of size...

    Please I need code in C++ Write a program that creates an int array of size 10, then uses a for-loop to populate the array with numbers from your favorite number pattern.

  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1,...

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