Question

C Programming: Functions Please answer 1 and 2 on paper in C code. Not C++ or...

C Programming: Functions

Please answer 1 and 2 on paper in C code. Not C++ or C# just C. If you could provide some comments with a brief explanation of why you did what you did, that would be very much appreciated. Thank you for your help!! :)

1. Write a function that takes a single float, and returns that number rounded as an integer (not truncated, rounded properly). Ex: round(4.5) -> 5, round (4.4) -> 4

2. Write a function that takes two lists of integers of the same length, and their length, and adds the second list to the first list. For example, add([1, 2, 3], [4, 5, 6]) -> [5, 7, 9]

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

Thanks for the question.


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>


int round(float num){
   // first find the integer part using the below line
   int integerPart = int(num);
   // subtract the integer part from the num to get the fractional part
   float decimalPart = num - integerPart;
  
   // now check the difference of the fractional part from 0.5 is greater than 0
   // if true return the integer part
   if((0.5-decimalPart)>0.0001) return integerPart;
   // else return integer part plus 1
   else return integerPart+1;
}


void add(int arr1[], int arr2[], int length){
   int i;
   // iterate each element in both the arrays
   for(i=0; i<length; i++){
       // add the elements and store the sum in the arr1 at that index
       arr1[i] = arr1[i] + arr2[i];
   }
  
}

int main(){
  
   printf("%.1lf when rounded gives %d\n",4.5,round(4.5));
   printf("%.1lf when rounded gives %d\n",4.4,round(4.4));
  
   int arr1[] ={1,2,3};
   int arr2[] ={4,5,6};
  
   int i;
   for(i=0; i<3; i++){
       printf("%d ",arr1[i]);
   }
  
   printf("\nAfter adding both arrays\n");
   add(arr1,arr2,3);
  
  
   for(i=0; i<3; i++){
       printf("%d ",arr1[i]);
   }

}

=================================================================

Add a comment
Know the answer?
Add Answer to:
C Programming: Functions Please answer 1 and 2 on paper in C code. Not C++ or...
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
  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • Haskell Programming 2. Returns the plural of a word by adding "s' unless the word ends...

    Haskell Programming 2. Returns the plural of a word by adding "s' unless the word ends in "y", in which can you remove the "y" and add "ies" 3. Write a function that takes two lists and returns a list of only those elements that appear in both lists. 4. Write a function that takes a list and an element and returns the number of times the element appears in the list. 5. Remove the leading spaces from a string...

  • Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with...

    Please help with all four questions regarding LISP Programming. Thank you. Please answer all questions with output plesase. LISP Programming Assignment It is a good idea to start this assignment early; Lisp programming, while not inherently difficult, often seem somewhat foreign at first, particularly when it comes to recursion and list manipulation. This assignment is loosely based on material by Dr. Henri Casanova.   Problem #1 Define a function that takes two arguments and returns the greater of the two. Problem...

  • C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the...

    C++ programming. Please provide copy/paste code and a screenshot of the code being ran in the compiler. Write a program that takes two rectangle objects and then adds them and returns the final rectangle object. You are required to overload the addition operator to add the two objects.

  • Intro to Programming and Logic: Python 2, Chapter 8 – Strings (so far have learned the...

    Intro to Programming and Logic: Python 2, Chapter 8 – Strings (so far have learned the way of a program, variables, expressions, statements, functions, interface design, conditional, recursion and iteration) Write a program that takes a string as a parameter. It will analyze the string and return True if it is a valid float number. It will return False if it is not a valid float value. Your function should return True in any of these cases: 0.17, .17, 5.27,...

  • c++ programming please. thx. Define a function template named "merge" to merge two lists of items...

    c++ programming please. thx. Define a function template named "merge" to merge two lists of items into one list including all the items of the first list followed by the items of the second list. Even though, the data type of the items of the lists is a generic type, both input lists include items of the same type. To merge the two lists, the function first dynamically allocates enough memory to accommodate the items of both lists. The allocated...

  • This is for programming using Python with the JES software. Please write the code with the...

    This is for programming using Python with the JES software. Please write the code with the proper indentation. Thanks! Problem 1: Write a function that takes 2 arguments (integer or float) and computes the sum, difference, product, and quotient of the arguments. Once you calculate the values you should print out statements telling the user what arguments we used to compute the values and what the result of each calculation (your function should produce 5 lines of output in the...

  • Write a C++ code based this question n this assignment you will create three additional functions...

    Write a C++ code based this question n this assignment you will create three additional functions for each one of the data structures created in class. You will be provided with a header file which you will modify and a demo program for each data structure. If your functions are implemented correctly the demo programs should compile and execute correctly. Part 0 (Comments) 1. Over the course of the semester we have discussed comments for each data structure. Please add...

  • Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or...

    Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or LISP if you prefer) . A function (binomial N k) that returns the binomial coefficients C(N, k), defined recursively as: C(NO) = 1, C(N, N) = 1, and, for 0<k < N E(N, k) = C(N-1, k) + C(N-1, k-1) 2. A function (mod N M) that returns the modulus remainder when dividing N by M 3. A function (binaryToDecimal b) that takes a...

  • PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3...

    PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3 (10 pts) Define a function rev that takes a list xs and returns it in the reverse order. • F# standard library has List.rev, do not use it. You need to reimplement it. In [ ]: let rev (xs: 'a list) In [ ]: // Test you function List.rev [] = rev [] |> printfn "%" List.rev [1..9] = rev [1..9] |> printfn "%b'...

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