Skip to content

Commit 18d603a

Browse files
committed
Files from the in class demo
1 parent 6b52156 commit 18d603a

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ function(create_test COM LINKLIST)
2626
endfunction()
2727
add_subdirectory(HelloWorld)
2828
add_subdirectory(CppUTest)
29+
add_subdirectory(InClassDemo)

InClassDemo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_executable(InClassDemo InClassDemo.cpp)

InClassDemo/FindFunction.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef FIND_FUNCTION_HPP
2+
#define FIND_FUNCTION_HPP
3+
4+
//Searches a given array, with size given for a value
5+
//and returns the index of that value
6+
template<typename T>
7+
size_t index_of(const T* array, size_t size, T searchFor)
8+
{
9+
for (size_t x = 0; x < size; x++)
10+
{
11+
if (array[x] == searchFor)
12+
{
13+
return x;
14+
}
15+
}
16+
return -1;
17+
}
18+
19+
template<typename T>
20+
size_t last_index_of(const T* array, size_t size, T searchFor)
21+
{
22+
for (size_t x = size - 1; x != (size_t)-1; x--)
23+
{
24+
if (array[x] == searchFor)
25+
{
26+
return x;
27+
}
28+
}
29+
return -1;
30+
}
31+
#endif

InClassDemo/InClassDemo.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include <stdint.h>
3+
#include "FindFunction.hpp"
4+
5+
6+
void getValues(int* x, int* y)
7+
{
8+
std::cout << "Enter two values: " << std::endl;
9+
std::cin >> *x;
10+
std::cin >> *y;
11+
}
12+
13+
int main(void)
14+
{
15+
//Console I/O
16+
std::cout << "Test" << std::endl;
17+
18+
float favorite;
19+
20+
std::cout << "Enter your favorite number: ";
21+
std::cin >> favorite;
22+
std::cout << "Your favorite number is: " << favorite << std::endl;
23+
24+
//Arrays
25+
const size_t array_size = 5;
26+
int array1[array_size] = {2,10,6,8,10};
27+
int array2[] = {5,10,15,20,25};
28+
static_assert(array_size == sizeof(array2)/sizeof(int),"");
29+
30+
std::cout << "The arrays are:" << std::endl;
31+
32+
for (size_t x = 0; x < array_size; x++)
33+
{
34+
std::cout << array1[x] << '\t' << array2[x] << std::endl;
35+
}
36+
37+
std::cout << "The index of 10 in the first array is: " << index_of(array1,array_size,10) << std::endl;
38+
std::cout << "The last index of 10 in the first array is: " << last_index_of(array1,array_size,10) << std::endl;
39+
40+
41+
//Dynamic Memory
42+
int* array3;
43+
size_t len;
44+
std::cout << "How long should the array be: ";
45+
std::cin >> len;
46+
array3 = new (std::nothrow) int[len];
47+
std::cout << "Enter the array values: " << std::endl;
48+
for (size_t x = 0; x < len; x++)
49+
{
50+
std::cin >> array3[x];
51+
}
52+
std::cout << "The array is: " << std::endl;
53+
for (size_t x = 0; x < len; x++)
54+
{
55+
std::cout << array3[x] << std::endl;
56+
}
57+
delete[] array3;
58+
59+
//Pointers
60+
int val1;
61+
int val2;
62+
getValues(&val1,&val2);
63+
std::cout << "You entered: " << val1 << " " << val2 << std::endl;
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)