Question

C Programming For this task, you will have to write a program that will prompt the...

C Programming

  1. For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature:
    int sum(int* arr, int n);

    1. Your code should look something like this (you can start with this as a template:
      int sum(int* arr, int n) {
      // your code for sum
      }

      int main() {
      int N;
      // some code to prompt for N
      int arr[N];
      // some code to prompt for N numbers
      int S = sum(arr, N);
      // print out S
      }

    2. Note that within sum, although arr is of the type int* (integer pointer), you can treat it as any other normal array

What the user should see in the console is something like this when the code is run (although formatting is up to you):
Number of numbers: 3
Input 1: 3
Input 2: 2
Input 3: 1
The sum of the numbers is: 6

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<stdio.h>

//method to find sum of elements in an array

int sum(int* arr, int n) {

                //setting sum to 0

                int s=0;

                //looping and summing up all numbers

                for(int i=0;i<n;i++){

                                s+=arr[i];

                }

                //returning sum

                return s;

}

int main() {

                int N;

                //prompting and reading N

                printf("Number of numbers: ");

                scanf("%d",&N);

                //declaring array

                int arr[N];

                //reading N numbers and adding to array

                for(int i=0;i<N;i++){

                                printf("Input %d: ",(i+1));

                                scanf("%d",&arr[i]);

                }

                //finding and displaying sum

                int s=sum(arr,N);

                printf("The sum of numbers is: %d\n",s);

                return 0;

}

/*OUTPUT*/

Number of numbers: 5

Input 1: 1

Input 2: 2

Input 3: 5

Input 4: 7

Input 5: 10

The sum of numbers is: 25

Add a comment
Know the answer?
Add Answer to:
C Programming For this task, you will have to write a program that will prompt the...
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
  • Program C: Start by printing out your name, then your code should prompt the user to...

    Program C: Start by printing out your name, then your code should prompt the user to enter integers using the following prompt: Enter some integers, end with a 0: Your code should: Level 1 Task: count the number of integers entered (not including the 0) Level 2 Task: produce the sum of all of the integers Level 3 Task: produce the sum of the even integers These calculated values need to be displayed as follows: There were ___ numbers read...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • please write c programming for this code below. Write a program to print a pattern of...

    please write c programming for this code below. Write a program to print a pattern of numbers separated by spaces for the given number of rows. At the time of execution, the program should print the message on the console as: Enter number of rows For example, if the user gives the input as: Enter number of rows : 4 then the program should print the result as: 232 34543 4567654

  • 1. Write and debug a MIPS program that performs the following operations . Prompt for and...

    1. Write and debug a MIPS program that performs the following operations . Prompt for and input three integers "a", "b and "c" using syscalls (3 points) Print all numbers starting from "a" upto "b" (including, but not exceeding "b") at an increment equal to value of"c"(5 points) If ba, then no numbers are printed, and a message is printed to inform it to the user. (3 points) Print the number and average of the generated numbers printed. (3 points)...

  • 1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or **...

    1. Prompt the user for one of the arithmetic operators ('op'): +, -, *,/, or ** 2. Prompt the user for two input integers ('a' and'b') 3. The input numbers can be positive or negative 4. Perform the arithmetic operation using the two input numbers 5. The first entered number ('a') is the left side, the second ('b') the right side of the operation For exponentiation, the first number is the base, the second the exponent Print (display) the following...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write a programming java that prints the sum of cubes. Prompt for and read two integer...

    Write a programming java that prints the sum of cubes. Prompt for and read two integer values from the user and print the sum of each value raised to the third power, i.e., (?3+?3). Sample output: (user input) Enter x as an int: 3 Enter y as an int: 4 (3^3) + (4^3) = 91

  • this assignment should be delivered in C language Program 4 will prompt the user for one...

    this assignment should be delivered in C language Program 4 will prompt the user for one or two token, space delimited, inputs of at most 20 characters. If the user provides more than 20 characters before a newline is reached print the provided error message. If the number of tokens is incorrect, print the appropriate error message. If the input is correct, print the appropriate token types. Prompt the user for input (and provide output) until the user provides a...

  • Write a code segment as specified below. // 1. Prompt the user to enter a positive...

    Write a code segment as specified below. // 1. Prompt the user to enter a positive integer number (say n). // 2. Calculate the sum of all the reciprocals of the numbers from 1 to n. For example, sum = (1 / 1) + ( 1 / 2 ) + ( 1 / 3) + ( 1 / 4 ) + … + ( 1 / n ) // 3. Print out the integer number and the sum. • NOTE:...

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