C/C++ :: Output From Array Is Incorrect?
Apr 25, 2015
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){
[code]....
View 1 Replies
ADVERTISEMENT
Oct 30, 2013
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
The output should be:
2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2
0 : 5, 1 : 1, 2 : 5, 3 : 6, 4 : 3
but I am getting:
1 2 4 0 4 4 3 3 2 4 0 0 1 2 1 1 0 2 2 1
0 : 4, 1 : 5, 2 : 5, 3 : 2, 4 : 4,
Here is my code:
#include<iostream>
#include<list>
using namespace std;
[Code].....
View 1 Replies
View Related
Nov 21, 2014
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.
Code:
char buff[BUFSIZ];
FILE *fp = stdout;
char * cstr = buff;
vector<std::string> vrecords;
while(std::fgets(buff, sizeof buff, fp) != NULL){
for(int i = 0; i < 6; ++i){
[Code] ....
expected output:
Found buy!
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
[Code] ....
View 14 Replies
View Related
Feb 2, 2015
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.
View 6 Replies
View Related
Oct 25, 2014
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;
[code].....
View 5 Replies
View Related
Oct 28, 2014
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);
View 1 Replies
View Related
Apr 6, 2014
I have this simple program below:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
} sensor;
[Code]...
As you can see, it has stored the last struct in all three indexes of the array. Why?
View 1 Replies
View Related
May 24, 2013
i having a code to pass in array as argument, but the length returned is 1. This is not match with the array size.
int Getsize(int Array[])
{
int len = sizeof(Array)/sizeof(int);
cout << len << "
";
}
int main()
{
int X[] = {45, 12, 54, 83, 41, 36};
getsize(X);
}
View 19 Replies
View Related
Feb 9, 2014
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:
RGB:(196,72,84)
HSL:(354,-46,52)
Correct HSL(354,51,53)
CODE:
double s=0;
double l=0;
chroma=max-min; //works correctly
//LIGHTNESS
l=(max+min)/2;
//SATURATION
if(chroma==0) {s=0;}
else {s=chroma/(1-fabs(2*l-1));}
s=s*100.000000;
l=(l/255.000000)*100.000000;
View 5 Replies
View Related
Apr 25, 2013
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.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct invRecord {
string delDate;
[Code] ....
View 7 Replies
View Related
Aug 6, 2014
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
void main()
{ char ch,c=0,che=16;
int i=1,j=16;
[Code] .....
View 6 Replies
View Related
Oct 30, 2014
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.
View 3 Replies
View Related
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()
[Code] .....
View 2 Replies
View Related
Mar 10, 2013
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.
View 3 Replies
View Related
Feb 9, 2015
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;
[Code] .....
View 5 Replies
View Related
Feb 15, 2014
I have this simple program below:
Code:
#include <stdio.h>
#include <limits.h>
unsigned int rightrot(unsigned int x, unsigned int n) {
/* calculate number of bits in type */
size_t s = sizeof(x) * CHAR_BIT;
[Code] ....
The result it prints is 2684356604 on my 32-bit computer. The result I expect is as follows:
0xFF94 is 0000000000000000 1111111110010100 in binary.
Shift it right by 5:
0000000000000000 0000011111111100
Then take that result in shift it right by 27 (s is 32 and p is 5, so the difference is 27):
1111111110000000 0000000000000000
Now we use bitwise or:
0000000000000000 0000011111111100 | 1111111110000000 0000000000000000 = 1111111110000000 0000011111111100
That in decimal is 4286580732. So how does it come up with 2684356604?
View 2 Replies
View Related
Oct 7, 2012
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.
View 4 Replies
View Related
Sep 21, 2014
So I have an array portfolio [i][j] where i and j are determined by inputs by the user. I can get the code to fill the array with the correct values; however, I can not figure out how to output the array to csv.
Is there a simple way to output to a csv? Is it easier to create the csv file and just write to it?
This is all I have so far:
for( i=0; i<sizevol; i++){
for(j=0; j<sizespot; j++){
myfile.open ('')
}
}
View 1 Replies
View Related
Feb 5, 2013
How do you use a for loop to iterate through strings to display them in a console?
View 3 Replies
View Related
Nov 10, 2014
At the bottom I have a loop that cout FI,XC,XL,I while going through the loop but when it prints its uneven and setw cant fix it. How do I print values of FI,XC,XL & I to an array so they are aligned.
#include <iostream>
#include <string>
#include <cmath>
#include "projectfunctions.h"
#include <iomanip>
using namespace std;
const double PI=acos(-1);
[Code] ....
View 4 Replies
View Related
Jan 1, 2014
Code:
#include main() { char why[64]; why =255,255,255,255,255,255,255,255;
while (why > 4); printf("why is equal to" ,why); why = why-2;
return 0;
}
It tells me incompatible data type.
View 9 Replies
View Related
Feb 8, 2014
I have an array of strings and a two dimensional array made up of floats. In the output for the strings (this array stores 10 divers numbers) I am getting only 8 of the numbers, then a core dump. What could cause this?
View 17 Replies
View Related
Feb 12, 2013
In the code that I am working on I am generating random numbers and assigning them a 2d array. But when I output the array in a separate nest of for loops the values are incorrect, but if I output the array in the same nest of for loops the values are correct
int spot[4][4];
int range[] = {1,16,31,46,61}, held[4];
void Generate() {
for (int y =0; y<=4; y++) {
for ( int x =0; x<=4; x++) {
spot[x][y] = (range[x] + rand() % 15);
[Code] ....
View 3 Replies
View Related
Apr 25, 2013
I want to create a function that will accept input from the user and return the input, to be used for further calculation. I know how to accept and return with integers as parameters , but how do i do it with arrays?
Suppose there is an array of numbers arr[]. Now, i want a function that accepts the input from the user, and return the array for further manipulation.
For example, if the array is arr[5], then i should call a function and accept the values from the user. Then, i should return the imputed values and print the same. How can i do this.
View 3 Replies
View Related
Feb 23, 2014
I been for trying to create 5x5 2d array that
basically display first column, last column , and diagonally (like " N " display). but i cant get it to work.
this is what i have so far.
int _tmain(int argc, _TCHAR* argv[]) {
char Test[5][5];
for (int i = 0; i < 5; i++)
[Code]....
View 4 Replies
View Related
May 3, 2015
Now this issue is about calculating the average of numbers and displaying it in the out put.
The following numbers is what I have read in from a file. There are 7 lines all the same format, just different values of course.
132475.889.392.3
435686.383.498.3
The output will need to look like:
Student #Test1Test2Test3Average
132475.889.392.385.8
435686.383.498.389.3
My program thus far is: (omitted code to shorten post and remove irrelevant information.
I don't know if it is because I have been working on other things and forgot how to proceed with the calculations and display, but I am just stuck. I'm not sure how to have the program read the numbers from columns 2 through 5 and then divide by 3. As you can see in my DisplayAverages() method I should have the ConsoleWrite and WriteLines done properly.
static void Main()
{
double[,] scoresArray = new double[7,4];
LoadFile();
ReadData(scoresArray);
[Code]....
Should read columns 2-4 and then display the average in column 5.
View 5 Replies
View Related