answersLogoWhite

0


Best Answer

46

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you Evaluate the expression 32-6 plus 5x4?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can we use gotoxy function in C plus plus?

Ideally you would not manipulate std::cout using such a low-level function. Console output is intentionally generic so that output can be easily redirected to any output device that accepts a character stream. As such, this code will not work correctly if the user were to redirect output to a disk file or a printer, or use the output as input to another program. If you wish to format screen output in a non-generic manner, do not use the console, use a screen-specific context device instead. #include<iostream> #include<string> #ifdef WIN32 #include<windows.h> #endif WIN32 void gotoxy(int x, int y) { #ifdef WIN32 static HANDLE h = NULL; if(!h) h = GetStdHandle (STD_OUTPUT_HANDLE); COORD c = { x, y }; SetConsoleCursorPosition (h,c); #elif linux printf("%c[%d;%df",0x1B,y,x); #else static_assert (false, "unsupported platform in gotoxy()"); #endif WIN32 } void draw_box(unsigned x, unsigned y, unsigned width, unsigned height) { gotoxy (x, y); std::cout << std::string (width, '*'); for (unsigned z=2; z<height; ++z) { gotoxy (x, ++y); std::cout << "*"; gotoxy (x+width-1, y); std::cout << "*"; } gotoxy (x,++y); std::cout << std::string (width, '*'); } int main() { draw_box(10,10,5,4); // draw 5x4 box at {10,10} gotoxy (0,25); }


Write a program using one print statement to print the asterisks triangle?

#include<stdio.h> #include<conio.h> int main() { int i,j,k,row,m; printf("Enter the no of rows:"); scanf("%d",&row); m=row; for(i=0;i<m;i++) { printf("\n"); for(k=0;k<row;k++) printf(" "); for(j=0;j<=i;j++) printf(" *"); row--; } getch(); }


How do you write a C plus plus program for the two dimensional array representation of a priority queue?

There are three ways to sort a two-dimensional array. We can sort each row so that the rows remain where they are but the elements within each row are sorted. Or we can sort the rows themselves so the elements within each row remain where they are but the rows are sorted. Or we can do both, sorting the elements in each row and then sorting the rows. The following program demonstrates all three methods using the same bubble-sort method (see the sort function near the top of the code). Keep in mind that a two-dimensional array is nothing more than a one-dimensional array of one-dimensional arrays. That is, array A[2][3] is a one-dimensional array of 2 elements where each element is a one-dimensional array of 3 elements. In other words, 2 rows with 3 elements per row. However, while static two-dimensional arrays must have the same number of elements in each row, dynamic two-dimensional arrays can have variable length rows. This is achieved by maintaining a one-dimensional array of pointers on the stack or on the heap, where each pointer refers to a separate row. The elements within each row must be contiguous, but the rows themselves can reside anywhere in memory, non-contiguously, and each row can have a different length. If the rows are the same length then you can access the individual elements just as you would with a static array using array subscripts. However, with variable length rows, you must dereference the row and determine its length before accessing the elements within that row. The program below uses vectors rather than arrays, but a vector is really nothing more than a one-dimensional dynamic array that knows its own size at all times. Vectors are therefore much easier to work with than arrays because all the memory allocations are done for us behind the scenes. A two-dimensional vector is nothing more than a vector of vectors which is easier to imagine as begin a vector of rows, where each row has variable length. However, the example below uses fixed-length vectors to implement an array of 5 rows with 4 elements per row. Due to the fact that every row must be contiguous within itself and that rows may have variable length, it is always going to be easier to sort two-dimensional arrays by row rather than by column. If you need to sort by column rather than row, rather than write a separate and needlessly complex function to cater for this, it is simpler to just orient your array in memory or on disk so that you can sort by row instead. Note that it is trivial to change the orientation of an array whilst printing the array (simply swap the inner and outer loops) so there is no need to physically store the data in the same orientation as the output. The only thing that actually changes is how you visualise the data: whether as a vector of horizontal rows (actual rows) or as a vector of vertical rows (actual columns). #include<iostream> #include<iomanip> #include<vector> #include<random> #include<time.h> template<typename T> void sort (std::vector<T>& A) { unsigned unsorted = A.size(); if (unsorted > 1) { do { unsigned new_size = 0; for (unsigned index=1; index<unsorted; ++index) { if (A[index]<A[index-1]) { std::swap (A[index], A[index-1]); new_size = index; } } unsorted = new_size; } while (unsorted); } } template<typename T> void print_array(std::vector< std::vector<T> >& arr) { for (unsigned row=0; row<arr.size(); ++row) { for (unsigned col=0; col<arr[row].size(); ++col) { std::cout << '\t' << arr[row][col]; } std::cout << std::endl; } std::cout << std::endl; } template<typename T> void randomise_array(std::vector< std::vector<T> >& arr) { // Pseudo-random number generator (range: 1 to 50). std::default_random_engine generator; generator.seed ((unsigned) time (NULL)); std::uniform_int_distribution<unsigned> distribution (1, 50); for (unsigned row=0; row<arr.size(); ++row) { for (unsigned col=0; col<arr[row].size(); ++col) { arr[row][col] = distribution (generator); } } } int main() { // Instantiate a 5x4 array std::vector< std::vector<unsigned> > original (5); for (unsigned row=0; row<5; ++row) original[row].resize (4); randomise_array(original); // Always work from a copy of the original std::vector< std::vector<unsigned> > copy = original; std::cout << "Original array:\n" << std::endl; print_array(copy); std::cout << "Sort each row individually:\n" << std::endl; for (unsigned row=0; row<copy.size(); ++row) sort (copy[row]); print_array(copy); copy = original; std::cout << "Original array:\n" << std::endl; print_array(copy); std::cout << "Sort the rows:\n" << std::endl; sort(copy); print_array(copy); copy = original; std::cout << "Original array:\n" << std::endl; print_array(copy); std::cout << "Sort each row individually and then sort the rows:\n" << std::endl; for (unsigned row=0; row<copy.size(); ++row) sort (copy[row]); sort (copy); print_array(copy); } Example output: Original array: 43 14 8 5 15 12 49 9 31 27 4 35 15 11 37 6 32 42 35 4 Sort each row individually: 5 8 14 43 9 12 15 49 4 27 31 35 6 11 15 37 4 32 35 42 Original array: 43 14 8 5 15 12 49 9 31 27 4 35 15 11 37 6 32 42 35 4 Sort the rows: 15 11 37 6 15 12 49 9 31 27 4 35 32 42 35 4 43 14 8 5 Original array: 43 14 8 5 15 12 49 9 31 27 4 35 15 11 37 6 32 42 35 4 Sort each row individually and then sort the rows: 4 27 31 35 4 32 35 42 5 8 14 43 6 11 15 37 9 12 15 49