C++ :: 2 Arrays - Vector Outputs Incorrect Answers
Oct 19, 2014
Program: I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers. It keeps outputting what looks like alt characters, why.
Here is the code:
#include <iostream>
#include <vector>
using namespace std;
int main()
Create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."
And here is the test data. I am posting the results from my desk-check table.
operation first integer second integer answer A 10 20 30 a 45 15 60 S 65 50 15 s 7 13 6 G -1 M 10 20 200 d 45 15 3 d 50 100 2
Then, I transferred my program into a source file. Here it is:
//Exercise16.cpp - display answer of two integers
#include <iostream> using namespace std; int main() { //declare variables int firstInteger = 0;
[Code] ....
After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.
The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers.
#include <iostream> using namespace std; const int QUESTION = 20; //class for testing grades class TestGrader {
My loop is outputting data incorrectly. I have inbound web data that come in sets of 7. I am trying to in insert the 7 records into a vector and then display the vector content followed by a new line.
Program:I have 2 arrays: 1 for the correct answers to a quiz, 1 for the user. I then have a vector to hold the incorrect answers.
It keeps outputting what looks like alt characters, why.
Here is the code:
#include <iostream> #include <vector> using namespace std; int main() { const char a1[]={'a','d','b','b','c','b','a','b','c','d','a','c','d','b','d','c','c','a','d','b'}; char a2[20]; int i=0; int incorrect=0;
Currently I have to manually look up values on several different tables in different locations. I have 8 or 10 tables with 100 to 500 parts.
I would like to write a program so if I enter a value it will return the corresponding correct answers from the tables.
If I entered 2.5 for a value it would return the following three items from the tables since they all meet the requirement.
Table 2 Part min max 235 2.4 2.9
Table 6 Part min max 589 2.3 2.5
Table 7 Part min max 12 2.3 2.7
What would the best method be for setting up and accessing tables like this? Is C++ good at representing something like this or should I be looking at a different language?
I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:
#include <iostream> #include <vector> using namespace std; int main() { int typeArray[4] = {55,66,77,88}; int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
Two Hard-coded Arrays into One 2-Dimensional Vector I have 2 arrays, each of them are hard-coded with integer values. I also have one 2-Dimensional vector and I want to put 1 array into the first column of the vector and the other array into the 2nd column of the vector. The reason is that I want to do math on the 2nd column of the vector only.
I am able to accomplish this with 3 arrays. Two of them are 1-Dimensional and the third array is 2-Dimensional.
Currently I am trying to convert RGB to HSL. Everything is working but the saturation value. It is always close to the correct value (usually less than 10 off). For example:
I am writing a program where I read in data from a file into an array and a 2D array. However, when I cout that data to insure that it was all read in correctly, I get only the first full line of that input file(where there are actually 25 rows and 12 columns).
What am I doing wrong or should be doing differently?
ifstream fin; //open the input file fin.open("store_data.txt"); //If input file was opened, read input file data into array and 2d array if(fin){
I'm writing a program to read in a Master.txt file and then update it through a Transaction.txt file that contains various transaction types [Adds (A), Deletes (D), and Edits (E1-E4)]. The records in both files are in ascending order based on Item#. Ultimately, the original Master.txt and updated Master file (Master2.txt) will be merged to reflect all valid transactions, and an errorLog.txt file will be created to indicate all invalid transactions. I feel I have all of the code written correctly, but I am still getting errors on my operands and identifiers.
PROGRAM:- #include<fstream.h> //for reading and writing files #include<conio.h> //for clrscr() #include<string.h> //for string characters #include<stdio.h> //for gets and puts function #include<process.h> //for exit function #include<iomanip.h> //for setw function #include<dos.h> //for delay and sleep function
Were are to implement a method countValue() that counts the number of times an item occurs in a linked list. Remember to use the STL <list>
int countValue(list<int> front ,const int item);
Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp.
In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.
Remember that all the above is to be included in the file ListP.ccp
I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value.
The relevant portion of the code is as follows.
unsigned int result = 0; long j = 0, length; unsigned char *data; data = (unsigned char *)malloc(sizeof(unsigned char)*800000);
[Code] ......
I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.
j = 62910, result = 64 j = 78670, result = 64 j = 100594, result = 64 j = 165658, result = 512 j = 247990, result = 128 j = 268330, result = 512 j = 326754, result = 1 j = 415874, result = 256 j = 456654, result = 1024 j = 477366, result = 512
It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.
I'm not changing the value of result anywhere else in the code, and when I print out (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab)), I get 0, but somehow when it gets stored in result, it's no longer zero.
We have to make a code to detect the frequency of printable characters. But when I run the code, sometimes it can't detect the Uppercase Letters. But it can sometimes. It's a bit buggy, and it really can't get the frequency of the space character.
Does textcolor affect the outcome?
Code: #include<stdio.h>#include<conio.h> int main () { clrscr(); char name[40],sentence[1000],ch; int c=0,count[95]={0}; textcolor(LIGHTCYAN);
[Code] ....
I also noticed that it stops detecting the frequency when there is a space between character/s.
I'd wrote a program to encrypt a message within a bmp file using my own structs and all for everything (yes, call me a ........head) The program works but for some weird ........ing reason I was forced to subtract 2 bytes from the header size to get the correct value. I've narrowed down the issue to my BmpFileHeader struct.
Here's a short program that demonstrates the issue:
Code: #include <stdio.h> #include <stdlib.h>
#define BYTE unsigned char #define WORD unsigned short #define DWORD unsigned long #define LONG signed int
[Code] .....
Tried with both gcc and TinyCC and got the same result so it doesent seem to be a compiler bug. Microsoft's structures though are giving the correct size, even though they have the exact same definition.
Microsoft's defines:
Code: // windef.h typedef unsigned long DWORD; typedef unsigned char BYTE; typedef unsigned short WORD;
This program has to convert an unsigned binary number into a decimal number. No matter what binary number I enter, however, it always outputs that the decimal number is 0.
My code is as follows:
#include <iostream> #include <cmath> #include <algorithm> using namespace std; int main() { string binarynumber; cout << "Enter an unsigned binary number up to 32 bits." << endl;
[Code] ....
And my output:
Enter an unsigned binary number up to 32 bits. 00001111 That number in decimal is 0
The output should have shown the binary number in decimal to be 15, and I cannot find my error.
I've implemented the merge sort algorithm and used the 'merge' part for counting the number of split-inversions in an array as part of an assignment for an online course. How ever, the out put array is not a sorted version of the input array and as a result the number of split inversions obtained is wrong. I think that there is some thing wrong in the way I am indexing arrays.
I've used ' cout ' to print the values of indexes to see exactly what values are being passed in during the recursions.
Code:
#include <iostream> using namespace std; int length=0,mid=0,inv=0; void mergesort(int arr[], int first, int last) { cout << "first: " << first << " " << "last: " << last; cout << endl;
I've been typed out a C program to let the user define the size of their string , and key in characters for this string , the program would then prompt the user for a character to search for in the string and return it's index value. Eg. Index of c in abc is 2. My code is as shown:
#include<stdio.h> #define SIZE 20 int search(char x[SIZE+1] , int n , char s); int main(void){ char x[SIZE+1] , s; int n , index;
[Code] ....
However , after I key in my characters for the string , the program does not prompt me to input a character to look for, it just prints it out and returns some funny number. But the program works just fine is I move this portion to the top :
printf("Enter alphabet to find: "); scanf("%c",&s);
It is getting more and more annoying, everytime i progress the next hurdle is just bigger. The problem is i want to calculate inverse of a matrix but using .inv gives a matrix full of -1.#QNAN values so i decided to write one my self
a function which returns the inverse matrix but something strange happens.
when i debug through the function code it works well and check the matrix that will be returned and the elements are correct.
when the function is called and output matrix is resulted. the element of the matrix is different.
I am new to C++ and I want to learn how to set color a particular color for the users input and output. For example, I want to display the users input as green, and their output as red.