Question

In: Computer Science

using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...

using only loops, no functions and no arrays.
with the heading:
#include <iostream>
using namespace std;

"Write a program that asks the user to enter an odd positive integer. The program reads a value (n) entered by the user and prints an n x n grid displaying a large letter X. The left half should be made up of pluses (+) and the right half should be made with the character "x" and the very center should be an asteric (*). If the value the user entered was not valid, the program should terminate."

Solutions

Expert Solution

Solution:

I have only used loops and if conditions to print the pattern

Code:

#include<iostream>
using namespace std;
int main()
{
        int n;
  cout<<"Enter an odd number: ";
        cin>>n;
        int i,j;
        for(i=1;i<=n;i++)
        {
                for(j=1;j<=n;j++)
                {
                        if(i==n/2+1 && j==n/2+1)
                        {
                                cout<<"*";
                        }
                        if(j<n/2+1 && (j == i || j == (n + 1 - i)))  
                        {
                                cout<<"+";
                        }
                        if(j>n/2+1 && (j == i || j == (n + 1 - i)))  
                        {
                                cout<<"x";
                        }
                        else
                        {
                                cout<<" ";
                        }
                }
                cout<<endl;
        }
}

Screenshot of Output and Code:


Related Solutions

Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std;...
Using only loops, no functions and no arrays. with the heading: #include <iostream> using namespace std; "Write a program that asks the user to enter an integer between 1 and 20. If the user enters an illegal number, the program repeatedly asks the user to enter the correct one. If the user has not entered a correct number after 10 attempts, the program chooses the number 10 as the user's number. The program prints the cube of the user's number.
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std;...
Complete the code provided to add the appropriate amount to totalDeposit. #include <iostream> using namespace std; int main() { enum AcceptedCoins {ADD_QUARTER, ADD_DIME, ADD_NICKEL, ADD_UNKNOWN}; AcceptedCoins amountDeposited = ADD_UNKNOWN; int totalDeposit = 0; int usrInput = 0; cout << "Add coin: 0 (add 25), 1 (add 10), 2 (add 5). "; cin >> usrInput; if (usrInput == ADD_QUARTER) { totalDeposit = totalDeposit + 25; } /* Your solution goes here */ else { cout << "Invalid coin selection." << endl;...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] =...
#include <string> using namespace std; //using recursion no loops allowed int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };   //Challenge #2: print the list from given range   printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6   cout << endl;   //Challenge #3: print the list, but backwards   printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8   cout << endl;                  //Challenge #4: reverse order of items in list   reverse(nums,...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT