Question

In: Computer Science

write a program which will prompt an array of 12 integers then print the array as...

write a program which will prompt an array of 12 integers then print the array as an array of 4 columns on 3 rows

Solutions

Expert Solution

/* First we will store these 12 integers in an array . After this we will create a two dimensional array of size 3 x 4 and then print the content of the 2 D array */

/*
1. Create a 1 dimensional array and insert any 12 elements into it.
2. create a dynamic 2 dimensional array of size 3 x 4
3. insert content of previous array into the dynamic array using
   array2d[i][j]=array1d[(j*row)+i]
4. print content of 2 D array two loops


if random numbers inserted into original array is :
   83 86 77 15 93 35 86 92 49 21 62 27
then ,its output will be like :
   83 15 86 21
   86 93 92 62
   77 35 49 27
  
   4 columns and 3 rows
*/

/* c++ code of the above algorithm */
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int arr1d[12];
   for(int i=0;i<12;i++)             // inserting any random no (0 to 99)
       arr1d[i]=rand()%100;           // into 1 D array
// you can take any integer values
   for(int i=0;i<12;i++)
       cout<<arr1d[i]<<" ";           //checking content of 1 D array
   cout<<endl;
  
   int row=3, column=4;               // forming a dynamic 2 D array of
   int *array2d[row];                   // given size(standard approach)
   for(int i=0;i<row;i++)
       array2d[i]=(int*)malloc(column*sizeof(int));
  
   for(int i=0;i<row;i++)               // copying contents from 1 D array
   {
       for(int j=0;j<column;j++)      
           array2d[i][j]=arr1d[(j*row)+i];
                                       // array2d[i][j]=ith row and jth column
   }
   for(int i=0;i<row;i++)               // Displaying contents of 2 D array
   {  
       for(int j=0;j<column;j++)
           cout<<array2d[i][j]<<" ";
       cout<<endl;
   }
   return(0);
}


Related Solutions

Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT