Question

In: Computer Science

Write a program in C that does the following: 1. Declares an array called numbers_ary of...

Write a program in C that does the following:

1. Declares an array called numbers_ary of 6 integer numbers.

2. Declares an array called numbers_ary_sq of 6 integer numbers.

3. Reads and sets the values of numbers_ary from the keyboard using a loop.

4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop.

5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop.

Example Output

Assume that both arrays have only 3 integer numbers and numbers_ary has [2 3 4]. Then,numbers_ary_sq will have [4 9 16] and the following would be displayed:

2 -> 4
3 -> 9
4 -> 16

Rubrics

  • Using arrays (3)
  • Using loops (3)
  • Correct logic (2)
  • Correct output (2)

Solutions

Expert Solution

Program:

#include<stdio.h>

int main()
{
  
//declaration of the two arrays
int numbers_ary[6];
int numbers_ary_sq[6];
int i;
  
//accepting the integers from user/keyboard
printf("Enter the 6 integer numbers : \n\n");
for(i=0;i<=5;i++)
{
scanf("%d",&numbers_ary[i]);
}
  
//storing the square of each integers of numbers_ary into numbers_ary_sq
for(i=0;i<=5;i++)
{
numbers_ary_sq[i]=numbers_ary[i]*numbers_ary[i];
}
  
//displaying the values of both the arrays besides each other
printf("\n\nThe values of numbers_ary and numbers_ary_sq: \n\n");

for(i=0;i<=5;i++)
{
printf("%d -> %d\n",numbers_ary[i],numbers_ary_sq[i]);
}
return 0;
}

Output:


Related Solutions

Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
C++ Write a program that declares two variables:a string firstName and int age.Write a function, called...
C++ Write a program that declares two variables:a string firstName and int age.Write a function, called getName, that when called, prompts the user for their first name. The function should return the first name and store it in the firstName variable. Write a function, called getAge, that when called, prompts the user for their age. The function should return the age and store it in the age variable. Write a function, that uses the firstName and age as arguments.In the...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
write the code that declares an array of booleans, called myarray, with size of 40 elements
write the code that declares an array of booleans, called myarray, with size of 40 elements
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
Write a C++program that initializes an array called resistances with the following initializer list: from file...
Write a C++program that initializes an array called resistances with the following initializer list: from file called "resistances. txt" {12.3, 98.5, 15.15, 135, 125} Using a while-loop, compute the sum of the elements of the resistances array and then obtain the average resistance and d isplay it. Then, use another loop to display the resistances that has value smaller than the average resistance that
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called...
PRACTICAL 10 C PROGRAMMING. Question 1 - Reading into a dynamic array. Write a program called temperatures01 that reads a (non-empty) sequence maximum daily temperatures. Your program should first ask for the number of temperatures to read and dynamically allocate an array just big enough to hold the number of temperatures you read. You should then read in the elements of the array using a loop. You then should print out the elements of the array in reverse order (from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT