Question

In: Computer Science

Only Program in C for this. No other programming language is allowed. Using a function, we...

Only Program in C for this. No other programming language is allowed.

Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as:

1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2

… and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:

8.9, 4.6, 7.8 and 995.1.

For this to work, I HIGHLY recommend using these guidelines:

1. Ask the user for the length of the array.

2. Ask the user for the starting and ending points.

3. Use the scan and print functions to populate the array.

4. Be sure to call the “adding values” function. This function must accept by 4 parameters: array’s length, the array, the starting point, and the ending point.

5. Inside your function, use these parameters to find the sum (use a loop! It's required, and any kind is fine).

6. When done, return the sum to main.

7. Print the returned value in main.

Solutions

Expert Solution

The C code:

#include<stdio.h>
//addingvalues function declaration
float addingvalues(int n,float arr[n],int start,int end)
{
   int j;
   float sum1=0.0;//initialization of sum1 to 0
   for(j=start-1;j<end;j++) //for loop for adding the values
   {
       sum1=sum1+arr[j]; //adding the array values
   }
   return sum1;//returns the final sum
}
int main()
{
   int n,i; //declaring n and i
   printf("Enter the length of the array:");
   scanf("%d",&n);//takes the length of the array from user
   float arr[n];//arr is declared as float
   printf("Enter the array values:");
   for(i=0;i<n;i++) //for loop for array values
   {
       scanf("%f",&arr[i]);
   }
   int l,r;//l for starting value and r for ending value
   printf("Enter the starting and ending points:");
   scanf("%d %d",&l,&r);//scans the input l,r from user
   float sum; //sum is declared as float
   sum=addingvalues(n,arr,l,r); //function calling
   printf("The sum is %.4f.",sum);//final sum printing
}

code snippet:

The output results:

for the given example

for another array

please see the code snippet for the indentation.

Please upvote this..


Related Solutions

You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT