1d C++
1d C++
1d C++
h> void main() { int j,i,k,A[4][4],B[16]; cout<<"enter the array:"; for(i=0;i<4;i++) for(j=0;j<4;j++) cin>>A[i][j]; cout<<"New Array: \n"; k=0; for(i = 0;i<4;i++) { for(j=0;j<4;j++) { B[k++] = A[i][j]; } } for(k=0; k<16; k++) cout <<B[k]<<"\n"; getch(); }
#include "stdafx.h" #include <iostream> #include <cctype> using std::cout; using std::cin; using std::endl; int main() { int grade[10]; int count = 0; char reply = 0; int sum = 0; do { cout<<endl; cout<<"enter the grades in whole numbers: ";
cin>>grade[count++]; cout<<"Do you want to enter the next grade (y/n)? "; cin>>reply; } while(count < 10 && std::tolower(reply) == 'y'); if(count == 10); cout << "maximum number of grades reached." << endl; double average = 0.0; for(int i = 0; i < count ; i++); average += grade[i]; average /= count; cout<<endl; cout<<"average grade is " <<average << cout<<endl; return 0; }
#include <iostream.h> int *Queue, front, rear, maxSize; void void void void createQueue(); push(); pop(); dispQueue();
int main() { int choice; while(choice != 5) { cout<<"\n\n Enter your choice :" <<"\n 1. Create an empty Queue." <<"\n 2. Push an element into Queue." <<"\n 3. Pop an element from Queue." <<"\n 4. Display the Queue." <<"\n 5. Exit the program.\n\n"; cin>>choice; switch (choice) { case 1: createQueue(); break; case 2: push(); break; case 3: pop(); break; case 4: dispQueue(); break; case 5: break;
case 6: cout<<"\n Invalid choice."; break; } } std::cin.get(); } void createQueue() { cout<<"\n Enter the maximum size of Queue : "; cin>>maxSize; Queue = new int[maxSize]; front=-1; rear=-1; } void push() { if (rear == (maxSize-1)) { cout<<"\n Overflow! No space left in Queue. Cannot push."; } else { rear++; cout<<"\n Enter the element to be pushed in Queue : "; cin>>Queue[rear]; } } void pop() { if (front == rear) { cout<<"\n Underflow! Cannot pop any items because Queue is empty."; } else { cout<<"\n The element that was popped was : "<<Queue[++front]; } } void dispQueue() { if (front == rear) { cout<<"\n Queue is empty, no element to display"; } else { for(int i = (front+1); i<=rear; i++) { cout<<Queue[i]; if (i != rear) {
cout<<"<--"; } } } }
#include <iostream> //for console i/o #include <fstream> //for file i/o #include <cmath> //for ceil() #include <string> //for strcat(), mostly using namespace std; int main() { //basic variable/array declarations: char filenameR[12]; char filenameW[12]; char char2write[2]; int divisions, counter, counter2, filepos; cout << "\n\nEnter the name of the file to divide (up to 8 characters for the name + period + 3 for the extension): "; cin >> filenameR; cout << "Number of files to divide the file into: "; cin >> divisions; cout << "\n"; //find file size: ifstream fileR(filenameR); fileR.seekg(0,ios::end); int filesizeR = fileR.tellg(); double filesizeW = ceil(filesizeR / divisions); //write the files: for (counter = 1; counter <= divisions; counter++) { filenameW[0] = char(48+counter); //starts at 1 then 2 then 3... char (48) is '0' filenameW[1] = '.'; filenameW[2] = 's'; filenameW[3] = 'p'; filenameW[4] = 'l'; filenameW[5] = '\0'; ofstream fileW(filenameW); cout << "Writing file " << filenameW << "...\n"; //writes one character per loop:
for (counter2 = 0; counter2 <= filesizeW; counter2++) { //set positions of "cursor" in fileR: filepos = counter2*counter; fileR.seekg(filepos,ios::beg); fileR.getline(char2write,2,'\0'); fileW << char2write; } cout << "Done writing " << filenameW << ".\n"; } cout << "Done.\n\n" << fileR; cin.ignore(); cin.get(); }