Skip to content

Instantly share code, notes, and snippets.

@edenizk
Created May 11, 2018 16:43
Show Gist options
  • Select an option

  • Save edenizk/0da1c563e21ce78b94d224901677ddc9 to your computer and use it in GitHub Desktop.

Select an option

Save edenizk/0da1c563e21ce78b94d224901677ddc9 to your computer and use it in GitHub Desktop.
playing with arrays
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
#include <windows.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void men(){
cout<<"\t\t ~ What you can do with these elements : \n"
<<"1 - Sum of all elements of this table \n"
<<"2 - Sum of squares of all elements of this table\n"
<<"3 - Multiplication of all elements of this table\n"
<<"4 - Arithmetic mean\n"
<<"5 - All elements of this table\n"
<<"6 - All elements of this table in reverse\n"
<<"0 - Exit";
}
//////////////////////////////////////////////////////
//////////////////////////////////////////////////////
void progressbar(){
cout<<"\t\t\t\t ";
/*Loading bar*/
for(int i=0; i<100/5; i++){
cout<<(char)177;
}
cout<<"<<\r\t\t\t\t";
for(int i = 0; i<=100; i++){
cout << "\r\t\t " << i << "% completed >>" ;
for (int o=1;o<i/5;o++){
cout<<(char)219;
}
switch (i% 4)
{
case 0: cout<<"\r\t\t/"; break;
case 1: cout<<"\r\t\t-"; break;
case 2: cout<<"\r\t\t\\"; break;
case 3: cout<<"\r\t\t-"; break;
}
Sleep(10);
}
cout<<endl;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
void listb(int a,int b[]){
cout<<endl<<"All elements of this table is : \n";
for(int i=a-1;0<=i;i--)
{
cout<<(-1*(i-a))<<"-"<<b[i]<<setw(80)<<"-position in array "<<i<<endl;
}
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
void list(int a,int b[]){
cout<<endl<<"All elements of this table is : \n";
for(int i=0;i<a;i++)
{
cout<<i+1<<"-"<<b[i]<<setw(80)<<"-position in array "<<i<<endl;
}
}
//////////////////////////////////////////////////
/////////////////////////////////////////////////
double art(int num[], int a , int &sum)
{
for (int i=0; i<a ; i++)
{
sum=sum+num[i];
}
sum=sum/a;
return sum;
}
//////////////////////////////////////////////////
/////////////////////////////////////////////////
double mult(int num[], int a , int &sum)
{
for (int i=0; i<a ; i++)
{
sum=sum*num[i];
}
return sum;
}
//////////////////////////////////////////////////
/////////////////////////////////////////////////
double sum(int num[], int a , int &sum)
{
for (int i=0; i<a ; i++)
{
sum=sum+num[i];
}
return sum;
}
/////////////////////////////////////////////////
////////////////////////////////////////////////
double sumsqu(int num[], int a , int &sum)
{
for (int i=0; i<a ; i++)
{
sum=sum+(num[i]*num[i]);
}
return sum;
}
///////////////////////////////////////
///////////////////////////////////////////
char againsaver(char again)
{
if((again=='n')||(again=='N'))
{
return again='n';
}
else if((again=='y')||(again=='Y'))
{
return again='y';
}
else
{
cout<<"\t-Please write [y/Y] for continue, and [n/N] for exit : ";
cin>>again;
// system ("CLS");
if((again=='n')||(again=='N'))
{
return again='n';
}
else if((again=='y')||(again=='Y'))
{
return again='y';
}
else
{
cout<<"\t -You kicked :P";
return again='n';
}
// cout<<"asd";
}
}
////////////////////////////////////////////////
///////////////////////////////////////////////
void bubbleb (int sort[], int a)
{
for (int i=0; i<a ; i++)
{
for(int j=0; j<a-1; j++)
{
if (sort[j]>sort[j+1]){
int tmp;
tmp = sort[j];
sort[j] = sort[j+1];
sort[j+1]=tmp;
}
}
}
cout<<endl<<"Bubble sorted : \n";
for(int i=a-1;0<=i;i--)
{
cout<<(-1*(i-a))<<"-"<<sort[i]<<setw(80)<<"-position in array "<<i<<endl;
}
}
////////////////////////////////////////////////
///////////////////////////////////////////////
void bubble (int sort[], int a)
{
for (int i=0; i<a ; i++)
{
for(int j=0; j<a-1; j++)
{
if (sort[j]>sort[j+1]){
int tmp;
tmp = sort[j];
sort[j] = sort[j+1];
sort[j+1]=tmp;
}
}
}
cout<<endl<<"Bubble sorted : \n";
for(int i=0;i<a;i++)
{
cout<<i+1<<"-"<<sort[i]<<setw(80)<<"-position in array "<<i<<endl;
}
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
int a=0, menu,b,c,d,f;
int sums;
char again;
do
{
system("CLS");
cout<<"\t - Please write how many elements you want me to create : ";
cin>>a;
cout<<"\t - what will be the min:(please enter a number)\n";
cin>>b;//rand max
//tester
if(cin.fail()||a<=0)
{
cin.clear();
cin.ignore(10, '\n');
cout<<"Choosen number is negative or not a number\n -Please write it again\n";
cout<<"please write your guess(it must be a number and it cannot be negative number):\n";
cin>>b;
if(cin.fail()||a<=0)
{
cout<<"Please next time try to write a number or positive number...";
break;
}
}
//tester
cout<<"\t - what will be the max:(please enter a number)\n";
cin>>c;//rand min
//tester
if(cin.fail()||a<=0)
{
cin.clear();
cin.ignore(10, '\n');
cout<<"Choosen number is negative or not a number\n -Please write it again\n";
cout<<"please write your guess(it must be a number and it cannot be negative number):\n";
cin>>c;
if(cin.fail()||a<=0)
{
cout<<"Please next time try to write a number or positive number...";
break;
}
}
//tester
if(c<=b)
{
cout<<"you confused about min and max i guess i will fix for you\n";
progressbar();
d=b;
f=c;
c=d;
b=f;
}
int nums[a],bnums[a],sort[a];
srand (time(NULL));
system("CLS");
cout<<"\t - Your numbers are :\n";
for(int i=0; i<a; i++)
{
nums[i] = b + rand () % (c-b+1);
sort[i]=nums[i];
}
progressbar();
list(a,nums);
bubble(sort,a);
cout<<"\t - Press any key to continue... ";
getch();
system("CLS");
do
{
system("CLS");
men();
menu=getch();
system("CLS");
switch (menu)
{
case 48:
return 0;
case 49:
sums=0;
sum(nums , a , sums);
progressbar();
cout<<"\t - Sum of all elements of this table is : "<<sums;
break;
case 50:
sums=0;
sumsqu(nums , a , sums);
progressbar();
cout<<"\t - Sum of squares of all elements of this table is : "<<sums;
break;
case 51:
sums=1;
mult(nums , a , sums);
progressbar();
cout<<"\t - Multiplication of all elements of this table is : "<<sums;
break;
case 52:
sums=0;
art(nums , a , sums);
progressbar();
cout<<"\t - Arithmetic mean is : "<<sums;
break;
case 53:
progressbar();
list(a,nums);
bubble(sort,a);
break;
case 54:
progressbar();
listb(a,nums);
bubbleb(sort,a);
break;
}
cout<<"\n\t-Do you wanna continue with same numbers?[Y/N]\n";
again=getch();
again=againsaver(again);
}while((again=='y')||(again=='Y'));
cout<<"\n\t-Do you wanna continue with different numbers?[Y/N]\n";
again=getch();
again=againsaver(again);
if((again=='n')||(again=='N'))
{
cout<<"Good Bye...";
}
}while((again=='y')||(again=='Y'));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment