Writing automated tests for unix/linux - c++

This is a constraint based question. I am using my school's server to SSH into. I have created a binary search program in C++. Here's the code:
#include <iostream>
using namespace std;
template<typename T>
bool bsearch(T num)
{
T arr[] = {5.3, 6.62, 7.74, 10.22, 13.22};
int len = (sizeof(arr)/sizeof(*arr));
int mid, l_bound=0, u_bound = len-1;
while (l_bound <= u_bound)
{
mid =(l_bound+u_bound)/2;
if (num > arr[mid])
l_bound = mid+1;
else if (num < arr[mid])
u_bound = mid -1;
else
return true;
}
return false;
}
int main()
{
float num;
cout <<"Number to search: ";
cin >>num;
if (bsearch(num) == true)
cout <<"Number found!\n";
else
cout <<"Nubmer not found!\n";
}
It's pretty simple. The only thing is: I want to write some unit tests for this, but I'm having problems getting any of the libraries on my schools SSH server. Is there another way to do this? When I try to include gtest and write this:
#include <gtest/gtest.h>
ASSERT_NE(1, 1);
It just gives me the error that
error: expected unqualified-id before 'switch'
error: expected unqualified-id before 'else'"
Is there a standard library for C++ that I can use? Or how could I get Google's test suite working on this?

Related

global vairable did not initialize correctly between running multiple test cases in c++

I am still actively learning c++ with a strong background in python3, the point of this question is not seeking any help with solving the problem Decode Variations on leetcode or pramp, but to understand the compilation or syntax related issue in c++.
The following code using dfs runs well if I run it case by case, however on pramp, it failed in RUN TESTS! Very surprising! It seems like in test case #2 int n=0; was not initialized and used the output of n in test case #1 as its value rather than 0, see the console in the attached screenshot at the end.
#include <iostream>
#include <string>
using namespace std;
int n=0;
void dfs(const string& s, int i){
if (i==s.size()){
n++;
return;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
dfs(s, i+2);
}
int decodeVariations(const string& s)
{
dfs(s,0);
cout<<n<<endl;
return n;
}
int main()
{
return 0;
}
Here is the code to run test case #2:
int main()
{
const string s = "26";
dfs(s,0);
cout<<n<<endl;
return 0;
}
If I added another initialization of n=0; to int decodeVariations(const string& s), then everything works fine. I try to become a programmer with a clear mind, please educate me.
Yes, non-const global variable is evil. Even though I don't know how leetcode and pramp (especially, the main function is empty) run a number of test cases, but I get a hunch it runs test case in the main function, which only compile and run the code once. Thus the global did not get reinitialized.
#include <iostream>
#include <string>
using namespace std;
int n=0;
void dfs(const string& s, int i){
if (i==s.size()){
n++;
return;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
dfs(s, i+2);
}
int decodeVariations(const string& s)
{
dfs(s,0);
return n;
}
int main(int argc, char **argv){
for (int i=1;i<argc;i++)
cout<<decodeVariations(argv[i])<<endl;
}
run with ./test 26 26 26
output:
2
4
6
Quick fix is to get rid of global variable
#include <iostream>
#include <string>
using namespace std;
//int n=0;
int dfs(const string& s, int i){
int ans = 0;
if (i==s.size()){
return 1;
}
if ( 0<s[i]-'0' && s[i]-'0'<10)
ans += dfs(s, i+1);
if (i+1<s.size() && 10<=stoi(s.substr(i,2)) && stoi(s.substr(i,2))<=26)
ans += dfs(s, i+2);
return ans;
}
int decodeVariations(const string& s)
{
// your code goes here
int n;
n = dfs(s,0);
cout<<n<<endl;
return n;
}

How to get a large decimal array working in binary search

I am currently trying to complete a project. This code is just a binary search algorithm that i have been using. This is my first search algorithm i have ever done, as i am a self learning programmer i need some advice on how to get this too work. I am using QT creator as an IDE.
I do not get any errors in my code. However, when i run the code the answer is always "No Match Found". Im thinking it has something to do with the array itself as i tested it with 10 integers before and worked perfectly fine.
I know im not very good at explaining this as my english is poor. But if you have any questions please get back to me.
Code Written in C++
#include <iostream>
using namespace std;
float binarySearch(float arr[], int left, int right, float x)
{
while (left <= right)
{
int mid = left + (right - left) / 2;
if (arr[mid] == x)
{
return mid;
}
else if (arr[mid] < x)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
int main()
{
float num;
float output;
float myarr[100] = {44.64978683, 44.62352548, 44.5972165, 44.57086043, 44.54445783,
44.51800925, 44.49151522, 44.46497629, 44.43839299, 44.41176587,
44.38509546, 44.35838228, 44.33162687, 44.30482976, 44.27799146,
44.25111251, 44.22419342, 44.1972347, 44.17023688, 44.14320046,
44.11612596, 44.08901388, 44.06186473, 44.03467901, 44.00745722,
43.98019985, 43.95290742, 43.9255804, 43.89821929, 43.87082457,
43.84339674, 43.81593628, 43.78844366, 43.76091937, 43.73336389,
43.70577768, 43.67816122, 43.65051497, 43.62283941, 43.595135,
43.56740221, 43.53964148, 43.51185328, 43.48403806, 43.45619628,
43.42832838, 43.40043481, 43.37251603, 43.34457246, 43.31660456,
43.28861275, 43.26059748, 43.23255917, 43.20449827, 43.17641519,
43.14831036, 43.12018421, 43.09203716, 43.06386963, 43.03568203,
43.00747477, 42.97924828, 42.95100295, 42.92273919, 42.89445742,
42.86615803, 42.83784141, 42.80950798, 42.78115811, 42.75279222,
42.72441067, 42.69601387, 42.6676022, 42.63917604, 42.61073577,
42.58228177, 42.55381441, 42.52533407, 42.49684112, 42.46833593,
42.43981886, 42.41129027, 42.38275054, 42.35420001, 42.32563904,
42.29706799, 42.26848721, 42.23989705, 42.21129785, 42.18268996,
42.15407372, 42.12544947, 42.09681755, 42.06817829, 42.03953203,
42.01087909, 41.98221981, 41.9535545, 41.9248835, 41.89620711};
while(true)
{
cout << "Please enter an element to search" << endl;
cin >> num;
output = binarySearch(myarr, 0, 100, num);
if (output == -1)
{
cout << "No Match Found" << endl;
}
else
{
cout << "Match found at position: " << output << endl;
}
}
}
The elements of your array myarr is sorted in decending order, so left should be set to mid+1 when current element is larger than target, not smaller.
Wrong:
else if (arr[mid] < x)
Correct:
else if (arr[mid] > x)
Also the array has only 100 elements, so the initial right should be 99, not 100.
Wrong:
output = binarySearch(myarr, 0, 100, num);
Correct:
output = binarySearch(myarr, 0, 99, num);

list requires class type

So I'm trying to make a bubble sort algorithm in class and I'm having this problem where it keeps giving me an error when I'm trying to find the length of the list where it says "expression must have a class type" and for the life of me I cannot figure out what to do. the tutorial I'm using isn't an help and I cannot find any other people with the same problem.
if anyone gets what it is asking I would appreciate the help, and any explanation would also be appreciated as I'm still new and would like to understand so I can try to learn
this was all done on VS 2017 (the free version)
#include "pch.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
bool found = true;
int target{ 0 };
int temp{};
bool ordered{ false };
int list[10] = { 4,6,5,1,3,2,10,8,9,7 };
cout << list.length() << endl;
bool swapped{ false };
while (ordered = false)
{
target = 0;
while (target != list.length)
{
if (list[target] > list[target + 1])
{
swapped == true;
list[target] = temp;
list[target] = list[target + 1];
list[target + 1] = temp;
target = target + 1;
}
else
{
target = target + 1;
}
}
if (swapped == false)
{
ordered = true;
}
}
cout << list << endl;
getchar();
return 0;
}
link to the photo of the error message
The error you have mentioned ("expression must have a class type") is caused by the below statement and other similar statements :
cout << list.length() << endl;
list is an integer array of size 10 as per this statement int list[10];
So you cannot use a . on it. You can use the . operator on a structure or class or union only. And even if list were a class/structure, length() method should be defined in it for the above to work.
Instead you should use sizeof operator. You can store it in a variable and use it later on.
size_t length = sizeof list/sizeof list[0];
cout << length << endl;

Is there a way to display my prime number list output in 2 columns?

I am taking my first programming class and this is my first time posting. I have been able to find help on this site for previous projects when I got stuck, and I hope I am doing this right.
I have completed the program below to display only prime number between 0 and 100 for my intro to C++ class.
The only thing is it kinda bothers me that it is in a single column, I wanted to go the extra step and make it look all nice and display the numbers in a couple columns. I tried using "\t", but I can't get it to work right. Any ideas on what I might add to my code?
I think I could do it using an array but we have not covered it in class and I'm not supposed to use them yet.
the challenge was:
"Use the isPrime function that you wrote in Programming Challenge 21 in a program that stores a list of all the prime numbers from 1 through 100 in a file."
and here is my code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
bool isPrime(int);
int main()
{
static int num1=0;
cout<<"Listed below is all prime numbers from 1 through 100."<<endl<<endl<<endl;
do
{
num1++;
if (isPrime(num1))
{
cout<<num1<<endl;
}
}
while (num1<100);
cout<<endl;
return 0;
}
bool isPrime(int num1)
{
bool primeNum=true;
for (int i=2;i<num1;i++)
{
if (num1%i==0)
{
primeNum=false;
}
}
return primeNum;
}
Thanks in advance for any input,
Find cout.width()
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
bool isPrime(int);
int main()
{
static int num1 = 0;
cout << "Listed below is all prime numbers from 1 through 100." << endl << endl << endl;
int column = 0; // column variable
int width = 10; // column width size
do
{
num1++;
if (isPrime(num1))
{
cout.width(width); // set column's width
cout << num1;
if (column == 1) { // if prime number is printed in column 2
cout << endl; // add new line
column = 0; // set column to first
}
else {
column++; // increase column index
}
}
} while (num1<100);
cout << endl;
return 0;
}
bool isPrime(int num1)
{
// error: your isPrime returns true when num1 is 1 or 2. change it
if (num1 == 1 || num1 == 2) return false;
// your isPrime
bool primeNum = true;
for (int i = 2; i<num1; i++)
{
if (num1%i == 0)
{
primeNum = false;
}
}
return primeNum;
}
I just realized the question asked for me to STORE the list to a file. So I rewrote and here is my new code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int num=0;
cout<<"This Program will store a list of only the prime numbers "<<endl;
cout<<"between 0 and 100 to the text file \"PrimeNumberList\"."<<endl<<endl;
cout<<"Find the list by using the file explorer to search for \"PrimeNumberList.txt\"."<<endl;
ofstream outFile;
outFile.open("PrimeNumberList.txt");
if (outFile.fail())
{
cout<<"Error opening \"PrimeNumberList.txt\" for output."<<endl;
return 1;
}
for (int i=1;i<100;i++)
{
if(isPrime(i))
{
outFile<<i<<endl;
}
}
return 0;
}
bool isPrime(int num1)
{
if (num1==1)return false;
bool primeNum=true;
for (int i=2;i<num1;i++)
{
if (num1%i==0)
{
primeNum=false;
}
}
return primeNum;
}

Number guesser using function and midpoint [duplicate]

This question already has an answer here:
Number Gusser in c++ using function and midpoint
(1 answer)
Closed 8 years ago.
I am writing a program for number guesser using function and midpoint to find out the number that been chosen. I have a problem compiling and I can't figure out the problem ????
The description of my problem:
The playOneGame function should have a return type of void. It should
implement a complete guessing game on the range of 1 to 100.
The shouldPlayAgain function should have a boolean return type. It
should prompt the user to determine if the user wants to play again,
read in a character, then return true if the character is a ‘y’, and
otherwise return false.
In addition, you should implement the helper functions
getUserResponseToGuess, and getMidpoint. They should be invoked inside
your playOneGame function.
getUserResponseToGuess. This function should prompt the user with the
phrase “is it ? (h/l/c): “ with the value replacing the token
. It should return a char. The char should be one of three
possible values: ‘h’, ‘l’, or ‘c’. It should have the following
signature: char getUserResponseToGuess(int guess)
getMidpoint. This function should accept two integers, and it should
return the midpoint of the two integers. If there are two values in
the middle of the range then you should consistently chose the smaller
of the two. It should have the following signature: int getMidpoint(int low, int high)
My code:
#include<iostream>
using namespace std;
void playOneGame;
char getUserResponseToGuess(int guess);
int getMidpoint ( int low, int high);
int main() {
do
{
playOneGame();
} while (shouldPlayAgain());
return 0;
}
void playOneGame
{
int a = 100;
cout << "\nGuess a number between 1 and 100. " <<endl;
getUserResponseToGuess ( a);
}
char getUserResponseToGuess(int guess)
{
while (true)
{
int guess = getMidpoint(minimum, maximum);
std::cout << "\nIs it [h]igher/[l]ower/[e]qual to " << guess << "? ";
char answer;
if (!(std::cin >> answer))
{
std::cerr << "error reading user input, program exiting\n";
exit(1);
}
if (answer == 'h')
minimum = guess + 1;
else if (answer == 'l')
maximum = guess - 1;
else if (answer == 'e')
{
std::cout << "Well, isn't that nice.\n";
return;
}
if (minimum > maximum)
{
std::cerr << "hey, you lied to me!\n";
exit(1);
}
}
}
int getMidpoint ( int low, int high)
{
int mid;
mid = (low + high) / 2;
return mid;
}
void playOneGame; is not a function forward declaration. void playOneGame(); is. Also the same applies for the function definition.
Also, you should define shouldPlayAgain() and include <stdlib.h> for exit() to work.
And getUserResponseToGuess() has just a return instead of returning something useful and it does not return anything on the default branch.