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...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the...
Write a program in C++ that declares an array of 100 integers named scores[]. Prompt the user for how many scores they want to enter. Then read in the specified number of ints and store them in the array. Then prompt the user for a passing grade. Use a for loop to go trough the array and count how many scores are passing. Print the count of how many passing scores, and also print a double that is the percent...
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...
write three functions in C++: one that declares a large array statically, one that declares the...
write three functions in C++: one that declares a large array statically, one that declares the same array on the stack, and one that creates the same array on the heap. Call each of them a number of times (at least 100,000) and output the time required by each. Explain the results.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT