C++ multiple char array comparison - c++

Okay so I am a bit stuck here with an assignment and really have no idea what to do next. The user is suppose input a series of key answers (A-D) and then have multiple students (the amount depends on the users input) input their own answers and the program should compare their answers with the answer key that was previously entered and score each student individually. Now my problem is, how do compare each student's answer with the answer key in the array? Here is my code so far
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int NumOfQ;
int NumOfS;
char TestAns[50];
string StudentNames[50];
int StudentScores[50];
char StudentAns[50];
int score[50];
cout << "Please enter the Number Of Questions\n";
cin >> NumOfQ;
cout <<"\nPlease enter your answers\n";
for(int x = 0; x < NumOfQ; x++){
cin >> TestAns[x];
}
cout <<"\nPlease enter number of students\n";
cin >> NumOfS;
cout <<"\nPlease enter Student's Names\n";
for (int s = 0; s < NumOfS; s++){
cin >> StudentNames[s];
}
for (int z = 0; z < NumOfS; z++){
cout<<"\nStudent: " << StudentNames[z] << " Please enter your test answers\n";
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
}
score[z] = 0;
for (int i = 0; i <=NumOfQ; i++){
if(TestAns[i] == StudentAns[i]){
score[z]++;
}
}
}
for(int Z = 0; Z < NumOfS; Z++){
cout <<"\Student : " << StudentNames[Z] << score[Z] << " out of " << NumOfQ << endl;
}
system("PAUSE");
return 0;
}

Scoring is very easy - you add one for every match in the answer and the key, like this:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
if (TestAns[i] == StudentAns[i]) {
score++;
}
}
You can do it with even less code if you recall that logical operations return zero for "true" and one for "false".
WARNING: This may cost you some readability points among your peers, or even some real points if your professor fails to get impressed by such brevity:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
score += (TestAns[i] == StudentAns[i]);
}
Case sensitivity is another concern: what if the answer key is c, and the user enters C? The above code would not give the student his hard-earned points, which is wrong. If you force the case of both characters to upper, mixed-case comparison would be correct:
if (toupper(TestAns[i]) == toupper(StudentAns[i])) {
score++;
}

Replace your last 'for' block with:
StudentScores[z] = 0;
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
if (TestAns[a] == StudentAns[a])
++StudentScores[z];
}

Related

Check for duplicate characters in C++ Array

I'm trying to create a program where you input 20 characters into an array. Each time you enter a new character, the program should check if that character is already in the array, if it is, print duplicate (but still add the duplicate to the array)
For example, if user types 'a', program should check if 'a' is inside the array.
This is my code:
#include <iostream>
using namespace std;
int main() {
char myAlpha[20];
char input;
cout << "Enter a letter" << endl;
for (int i= 0; i <= 20; i++) {
cin >> input;
for (int k = 0; k <= 20; k++) {
if (myAlpha[k] == input)
cout << "Duplicate" << endl;
}
myAlpha[i] = input;
}
}
I just can't figure out how to make it work, I'm probably missing something stupid. The solution must use something like the code above, no fancy functions or anything.
EDIT: Fixed Code:
#include <iostream>
using namespace std;
int main() {
char myAlpha[20];
char input;
cout << "Enter a letter" << endl;
for (int i= 0; i < 20; i++) {
cin >> input;
for (int k = 0; k < i; k++) {
if (myAlpha[k] == input)
cout << "Duplicate" << endl;
}
myAlpha[i] = input;
}
}
Only problem is that "duplicate" is printed a extra time for each duplication of a letter. For example, if 'a' is entered 3 times, on the 3rd time, "duplicate" is printed 2 times. And so on. But not a big deal.
try this:
#include <iostream>
using namespace std;
int main() {
char myAlpha[20];
char input;
cout << "Enter a letter" << endl;
for (int i = 0; i < 20; i++) {
cin >> input;
myAlpha[i] = input;
for (int k = 0; k < i; k++) {
if (myAlpha[k] == input)
cout << "Duplicate" << endl;
}
}
}
With this line:
char myAlpha[20];
The valid indices of that array are from 0 to 19. However, this line:
for (int i= 0; i <= 20; i++) {
Already implies a bug. Change it to this:
for (int i= 0; i < 20; i++) {
Do the same treatment for the inner for loop line: for (int k = 0; k <= 20; k++) {
But... you'll also need to keep track of how many characters have been inserted instead of looping up to 20 again.
So this is closer to what you want:
int main() {
char myAlpha[20];
char input;
cout << "Enter a letter" << endl;
int inserted = 0;
while (inserted < 20) {
cin >> input;
bool duplicate = false;
for (int k = 0; k < inserted; k++) {
if (myAlpha[k] == input) {
cout << "Duplicate" << endl;
duplicate = true;
break; // no point in continuing to look for duplicates once the first one is found
}
}
if (!duplicate) {
myAlpha[inserted] = input;
inserted++;
}
}
}
Even faster and you can avoid the inner for-loop:
int main() {
bool duplicates[256] = {};
char myAlpha[20] = {};
char input;
cout << "Enter a letter" << endl;
int inserted = 0;
while (inserted < 20) {
cin >> input;
bool duplicate = duplicates[(unsigned char)input];
if (duplicate) {
cout << "Duplicate" << endl;
}
else {
myAlpha[inserted] = input;
inserted++;
duplicates[(unsigned char)input] = true;
}
}
}

Equivalence Relation

I am stuck on what to do next... The program is suppose to check to see if entered Zero-One Matrix is an Equivalence relation (transitive, symmetric, and reflexive) or not. I am still new to C++ (started this semester). I know how to create the matrix using vector but not on how to check if it is equivalence relation or not..
I assume I need to use boolean function but I'm stuck on what I need to put in as an argument or if this is correct. My original thought was... so for symmetric it will look like (which I know this goes after #include and beofre int main(). Any help would be awesome.
bool isSymmetric(vector<int> &vect, int Value)
{
for (int i = 0; i < Value; i++)
for (int j = 0; j < Value; j++)
if (vect[i][j] != vect[j][i])
return false;
return true;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< vector<int> > vec;
cout << "NxN matrix N: ";
int Value;
cin >> Value;
cout << Value << "x" << Value << " matrix\n";
for (int i = 0; i < Value; i++) {
vector<int> row;
for (int j = 0; j < Value; j++) {
cout << "Enter a number (0 or 1): ";
int User_num;
cin >> User_num;
while (User_num != 0 && User_num != 1) {
cout << "Invalid Entry! Enter 0 or 1!\n";
cout << "Enter a number (0 or 1): ";
cin >> User_num;
}
row.push_back(User_num);
}
vec.push_back(row);
}
cout << endl;
for (int i = 0; i < Value; i++) {
for (int j = 0; j < Value; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

Controlling input in c++ and display

Question :
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
Example
Input:
1
2
88
42
99
Output:
1
2
88
So that is the question, however i am still a beginner and unable to have an input tab like that. In my program, how should i modify it such that it still accepts numbers after 42, however, it does not print them? currently I am only able to terminate the input at 42.
#include <iostream>
using namespace std;
int main()
{
int A[100], num, i=0,k,count;
for(count = 0; count != 1;){
cin >> k;
if (k!=42){
A[i] = k;
i++;
}
else
count =1;
}
cout << endl;
for (count = 0; count <i; count ++){
cout << A[count] << endl;
}
}
You don't have to use array at all. You can print the value just after reading it. Exit when you read 42. This may help you.
#include <iostream>
using namespace std;
int main() {
// your code goes here
int n ;
for(; ;) {
cin >> n ;
if(n == 42) {
return 0 ;
}
cout << n << endl ;
}
return 0;
}
Pretty sure the easiest way to do so is to simply ask the user how many numbers they need to enter.
#include <iostream>
using namespace std;
int main()
{
int A[100], k, count;
cout << "How many numbers do you want to enter ? ";
cin >> count; //this is to count how many numbers the user wants to enter
for(int i(0); i < count; ++i) //put all the numbers user enters in your array
{
cin >> k;
A[i] = k;
}
cout << endl;
for (int i(0); i < count; ++i)
{
if (A[i] == 42) //if the element at index i is == 42 then stop displaying the elements
break;
else
cout << A[i] << " "; //else display the element
}
cout << endl;
return 0;
}
Else you would need to put everything in a string and parse it and i'm not quite sure how that goes as I am a beginner as well.
EDIT:
Actually here you go, I think that is correct and does exactly what you want.
Do keep in mind that if user enters p.e "1 88 442" it will output "1 88 4" because it found "42" in "442". But it should be okay because you precised input numbers should only be two digits max.
#include <iostream>
using namespace std;
int main()
{
string k;
getline(cin, k);
cout << endl;
for (unsigned int i(0); i < k.length(); ++i)
{
if (!((k[i] == '4') && (k[i+1] == '2'))) //if NOT 4 followed by 2 then display
cout << k[i];
else
break; //else gtfo
}
cout << endl;
return 0;
}
Use a bool value to control the execution of your code.
#include <iostream>
#define N_INPUT 100
#define THE_ANSWER 42
using namespace std;
int main()
{
int array[N_INPUT], i, input, count=0;
bool universeAnswered = false;
for (i = 0; i < N_INPUT; i++) {
cin >> input;
if (!universeAnswered)
{
if (input == THE_ANSWER) {
universeAnswered = true;
} else {
array[count] = input;
count++;
}
}
}
for (i = 0; i < count; i++) {
cout << array[i] << endl;
}
}
(My code was not tested)
You just have to have some state to see if you have seen 42 already, and only output if you haven't
#include <iostream>
int main()
{
bool output = true;
for (int n; std::cin >> n;)
{
output &= (n != 42);
if (output)
{
std::cout << n << std::endl;
}
}
return 0;
}

why does my counter increase according to the lens of char input in C++

I'm picking up on C++ recently and is trying to code a program which prompts for names for a defined no. of times and inserts each of the input into an array of size-5. The problem happened when I tried to run the following code, my counter, i increases according to the no of len the user input. Why is that so?
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
char name[SIZE];
int i;
for (i = 0; i < SIZE; i++){
if (strlen(name) <= 50) {
cout << "Enter a name: \n";
cin >> name[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
Output:
if (strlen(name) <= 50) {
You should not call strlen on array which is not initialized.
Use array of strings otherwise
cout << name[i] << endl;
refers to i-th character, not entire string. Or if you want to go with char arrays, you'd need a two dimensional array.
I thing what you indended to do was :
#include <iostream>
using namespace std;
int main(){
const int SIZE = 5;
string names[SIZE];
int i;
for (i = 0; i < SIZE; i++){
cout << "Enter a name: \n";
string name;
cin>>name;
if (strlen(name) <= 50) {
cin >> names[i];
}
}
for (i = 0; i < SIZE; i++){
cout << name[i] << endl;
}
return 0;
}
UNTESTED
The second for loop, which does the output, does this in single characters, incrementing i each time.
To output the string all at once assign a string pointer to name[0] and send that to cout.

C++ calender, closes before I can see output

So basically I created this calender and when it outputs you can briefly see that it's right but it just closes down, can someone help me to keep it open to display the output?
I know I should know what this is but this took me a long time to do and my mind just isn't in the right place. thanks
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include "float.h"
#include <stack>
using namespace std;
using std::stack;
int calendar[6][7];
void cal(int y, int z) // y is number of days and z is the number corresponding
{ // to the first day
int n = 1;
for (int j = z - 1; j<7; j++)
{
calendar[0][j] = n;
n++;
}
for (int i = 1; i<6 && n <= y; i++)
{
for (int k = 0; k<7 && n <= y; k++)
{
calendar[i][k] = n;
n++;
}
}
}
int main()
{
int d;
int day;
cout << "Enter number of days : ";
cin >> d;
cout << "Enter first day of the month(1 for monday 7 for sunday..) : ";
cin >> day; cout << "\n";
cal(d, day);
cout << "M T W T F S S" << endl;
cout << "\n";
for (int i = 0; i<6; i++)
{
for (int j = 0; j<7; j++)
{
cout << calendar[i][j] << "\t";
}
cout << "" << endl;
}
}
Use std::cin.get() to keep the window open.
Since noone mentioned it - you can also do system("pause") on the Windows system family. It does what you need in the most 'elegant' way.
The easiest way is probably to ask for some input right before the end of main:
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
std::cin.ignore();
This will wait for the enter key to be hit: since there was just some numeric input done, there is still, at least, a newline in the input buffer. The first line gets rid of any already entered line. The second line then waits for the enter key.
I usually just use getchar() to wait for user input, which is shorter than std::cin.get().