Question

C Programming Given the following two arrays, write a for loop that copies all values in...

C Programming

  1. Given the following two arrays, write a for loop that copies all values in the first array that are greater than 50 into the second array. The contents of the second array should not skip elements, so the final contents of a2 should be {59, 61, 94, 76, 89, 0, 0, 0, 0, 0}. Note: Consider having two integer counters – one for the loop control variable that will access the elements of the source array, a1, and a separate one for the index of a2 that will only update when an element has been added.

short a1[10] = { 46, 13, 59, 61, 27, 23, 94, 76, 38, 89 };

short a2[10] = {0};

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>


int main() { 
   short a1[10] = { 46, 13, 59, 61, 27, 23, 94, 76, 38, 89 };

   short a2[10] = {0};
   int i, j = 0;
   
   for(i = 0;i<10;i++){
      if(a1[i]>50){
         a2[j++] = a1[i];
      }
   }
   
   for(i = 0;i<10;i++){
      printf("%d ",a2[i]);
   }
   return 0; 
}
Add a comment
Know the answer?
Add Answer to:
C Programming Given the following two arrays, write a for loop that copies all values in...
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
  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...

    Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays using pointers. Input:     4     1 2 3 4     5     4 5 6 7 8     where: First line represents the number of elements in the first array. Second line represents the elements in the first array. Third line represents the number of elements in the second array. Fourth line represents the elements in the second array. Output:     4 5 6 7...

  • Write a java method merge that accepts two arrays of integers and returns a new array...

    Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10};   int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • using C programming Compose a program that declares two string arrays that are 81 characters in...

    using C programming Compose a program that declares two string arrays that are 81 characters in size. Prompt the user to enter in a string and store it in array 1. Construct a function that returns a void and takes both arrays as parameters and copies array 1 into array 2 using sizeof() to ensure that the size does not exceed the destination array. Back in the caller print the second array to show that the function worked. NOTE: We...

  • Given the following parallel arrays, write code (using a For Loop) to traverse the arrays, add...

    Given the following parallel arrays, write code (using a For Loop) to traverse the arrays, add the contents of intSecondNum to the contents of intFirstNum and put each answer in the intAnswer array. Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click Dim intFirstNum()      As Integer = {4, 15, 8, 0, 13, 28, 5} Dim intSecondNum() As Integer = {3, 18, 21, 6, 8, 9, 8} Dim intAnswer(6) As Integer For intX = 0 to intFirstNum.Length -1 <==your code...

  • C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop...

    C programming only please 5.2.3: Printing array elements with a for loop. Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1. (Notes) Note: These activities may test code with...

  • Language: C++ Course: Computer Programming for Engineers Write a program that creates two 3x4 arrays of...

    Language: C++ Course: Computer Programming for Engineers Write a program that creates two 3x4 arrays of type "int". Populate each array with random integers between 1 and 9. Also ask the user to choose whether they want to add the arrays or subtract the second array from the first array. Output all three arrays in tabular form.

  • 4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized...

    4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive...

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