Académique Documents
Professionnel Documents
Culture Documents
#include <iostream>
#include <iomanip>
using namespace std;
int RandomInt(int minvalue, int maxvalue);
double RandomDouble(double minvalue, double maxvalue);
void swap(int a, int b);
int i = 10;
int main()
{
//mess
int d = 1, e = 2;
swap(d,e);
cout << "d = " << d << " and " << " e = " << e << endl << endl;
//seed rand
srand((unsigned int)time(nullptr));
//output random numbers
int randnum = 0;
randnum = RandomInt(0,10);
RandomInt(0,10);
RandomInt(0,10);
RandomInt(0,10);
system("PAUSE");
cout << "The value is: " << randnum << "\n\n";
int i = 0;
for(i = 0; i < 100; i++)
{
cout << RandomInt(2,10) << ' ';
if((i+1)%10==0)
cout << '\n';
}
//messing with scope
{
int i = 4;
cout << "\n\n i = " << i << "\n\n";
cout << "\n\n global i = " << ::i << "\n\n";
}
cout << "\n\n i = " << i << "\n\n";
cout << "\n\n global i = " << ::i << "\n\n";
//more random numbers
cout << "\n\n\n\n";
for(i = 0; i < 200; i++)
{
cout << RandomDouble(2.3,7.2) << ' ';
if((i+1)%5==0)
cout << '\n';
}
system("PAUSE");
return EXIT_SUCCESS;
}
void swap(int p, int q)
{
int temp = p;
p = q;
q = temp;
}
int RandomInt(int minvalue, int maxvalue)
{
static int i = 5;
//cout << "RandomInt i = " << i << endl;
i++;
int range = maxvalue - minvalue + 1;
int val = rand() % range;
return val + minvalue;
}
double RandomDouble(double minvalue, double maxvalue)
{
double range = maxvalue - minvalue;
double val = rand()/(RAND_MAX + 1.0);
return val*range + minvalue;
}
POINTER CODE
#include <iostream>
#include <iomanip>
using namespace std;
void swap(int *a, int *b);
void example();
void outputstr1(const char s[]);
void outputstr2(const char *s);
//void outputstr3(char * const s);
int main()
{
//declaration
int *p, i = 2;
cout << "1) i = " << i << endl;
p = &i;
*p = 4;
cout << "2) i = " << i << endl;
//function calls
int j = 1;
cout << "\nbefore: i= " << i << "and j = " << j << endl;
swap(&i, &j);
cout << "after: i= " << i << "and j = " << j << endl;
//arrays
char str[80] = "At the end of the day like", *ptr = NULL;
ptr = str;
cout << ptr << endl;
//erroneous code
//str = ptr;
*ptr = 'I';
cout << ptr << endl;
//*(ptr + index) == array[index]
*(ptr + 1) = 's';
cout << ptr << endl;
//use array notation
ptr[3] = 'T';
cout << ptr << endl;
//run the example
example();
outputstr1("The end is nigh");
outputstr2("The night will end");
//outputstr3("Yeah, yeah, yeah");
system("PAUSE");
return EXIT_SUCCESS;
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void example()
{
int count[4] = {10,5,3,7};
int *ptr;
ptr = count;
*ptr = 4;
cout << "*ptr = "
cout << "*(ptr+2)
ptr = ptr + 1;
*ptr = 8;
cout << "*ptr = "
cout << "*(ptr+2)
ptr++;
*ptr = 12;
cout << "*ptr = "
ptr -= 2;
cout << "*ptr = "
}
void outputstr1(const char s[])
{//const means string contents cannot be modified
//s[0] = 'E';
int i = 0;
for(i = 0; s[i]; i++)
cout << s[i];
cout << endl;
}
void outputstr2(const char *s)
{//const means string contents cannot be modified
//*s = 'E'.
while(*s)
cout << *s++;
cout <<endl;
}
/*
void outputstr3(char * const s)
{//const here means pointer is constant and the address referred to cannot
be modified
//*s = 'E'.
while(*s)
cout << *s++;
cout <<endl;
}
/
MORE POINTERS
#include <iostream>
#include <iomanip>
using namespace std;
void findsum_ptr(double *data, int n);
void findsum_arr(double *data, int n);
void set_values(double *data, int n);
void calcmean(double *data, int n, double *result);
int main()
{
//grab some memory
double *mydata = nullptr;
double mean = 0;
mydata = new double[100];
*mydata = 5;
mydata[4] = 10;
*(mydata + 6) = 20;
set_values(mydata, 100);
calcmean(mydata, 100, &mean);
cout << "mean = " << mean << endl;
//give back the memory
delete [] mydata;
//efficiency
//grab some memory
double *pdata = new double[5000];
set_values(pdata, 5000);
int m = 0;
clock_t start = clock();
for(m = 0; m < 1000000; m++)
findsum_arr(pdata, 5000);
clock_t end = clock();
cout << "Array took : " << (end - (double)start)/CLOCKS_PER_SEC <<
" seconds " << endl;
start = clock();
for(m = 0; m < 1000000; m++)
findsum_ptr(pdata, 5000);
end = clock();
cout << "Pointer took : " << (end - (double)start)/CLOCKS_PER_SEC
<< "seconds " << endl;
delete [] pdata;
system("PAUSE");
return EXIT_SUCCESS;
}
MORE POINTERS
#include <iostream>
#include <iomanip>
using namespace std;
void calcsum(int *data, int ndata,
int *sum);
int main()
{
char *str1 = new char[100];
char *str2 = new char[100];
//method1 stops taking input after space
cin >> str1;
cin.clear();
cin.ignore(INT_MAX,'\n');
INPUT CHECK
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int i = 0, val = 0;
while(i < 3)
{
cout << "Please enter an integer value: ";
if(!(cin >> val))
{
if(i < 1)
cout << "\nInput failure - please try
again\n";
else
cout << "\nInput failure - last chance to
get it right!\n";
//next two lines clear out the input buffer
cin.clear();
cin.ignore(INT_MAX, '\n');
i++;
cout << endl;
}
else
break;
}
cout << "\nYou entered " << val << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}