It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'd like to add an int from a struct with a regular int in c++. Is there an easy way to do it? Ive searched pretty much everywhere but theres nothing on adding either two struct ints while reading in data from a binary file or adding a regular int and a struct int together.
This is the simple version of what I currently have.
struct Add{
int k;
};
int total;
Add a;
//read in first set of number from binary file
total += a.k;
//add up to total, then read in second set of number from binary file.
The problem is, when I output total, it only gives me the last number I tried adding int k to it and not the total.
My actual code as requested.
struct TaskInit{
int weight;
};
TaskInit t;
int totalWeight;
for (int i = 1; i <= noOfRecords; ++i)
{
afile.seekg ((i - 1) * sizeof (TaskInit), ios::beg);
afile.read (reinterpret_cast <char *>(&t), sizeof (t));
totalWeight += t.weight;
}
cout << totalWeight;
struct Add{
int k;
};
int total = 0; // no indeterminate values. always init locals!
Add a;
// open your file here.
while (inFile >> a.k) {
//read in first set of number from binary file
//add up to total, then read in second set of number from binary file.
total += a.k;
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
im trying to find multiples of an array using a loop
int array[11] = {1,2,3,4,5,6,7,8,9,10,11};
int size=11;
for(int i=0;i > size;i++)
{
if (i%2==!0)
cout << array[i];
}
why wont this work
//Declare num first.
//Correct if condition..
//correct condition checking in for loop
int array[11] = {1,2,3,4,5,6,7,8,9,10,11};
int index=2;
int size=11;
int num = 5;
for(int i=0;i < size;i++)
{
if (i%num==0)
cout << array[i];
}
I think you might want to change the (somewhat ... unusual):
if (i%2==!0)
into:
if (i % 2 == 0)
In addition, you loop termination condition is such that the loop will never execute. Try:
for (int i = 0; i < size; i++)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
This is my first question on Stackoverflow, so please excuse me if I'm doing something wrong. :) Can you help me with this code? Thank you! :)
#include <iostream>
using namespace std;
1)
int divisor(int a) // function that checks the divisors of a number *)
{
int i=1; // counter
while(i<=a) // while the counter is less than the number given
{
int i;
if(primes(i)) // here i get the error "primes was not declared in this scope
{
if(a%i==0) // if the remainder of the operation is 0, the number is a divisor
{
cout<<"The divisors of the number are: "<<i<<endl; // prints the divisors
++i; // counter is incremented
}
}
else // else it is not, and the counter i is incremented
{
++i;
}
}
}
2)
int primes(int number) // checks if a number is prime
{
int i;
for(i=2;i<number;i++) // for loop until the counter i is less than a number
{
if(number%i==0) // if the remainder of the operation is 0, the number is not prime
{
break;
}
else //else the number is prime
cout<<number;
break;
}
}
}
3)
int main()
{
int n,i;
cout<<"Give the number: ";
cin>>n;
for(i=2;i<n;i++)
{
divisor(n);
}
}
There are several issues:
1) You need to forward-declare primes() before divisors():
int primes(int number);
2) Your primes() function fails to return a value.
3) Not all code paths in divisor() increment i.
I am sure there are more problems, but this should get you started...
Assuming that the order you give above is the same order as you use in the file, the problem is that your divisor function comes first and makes use of the primes function which is declared later. The easiest (and I guess also most common) solution is to use "foward-delcaration". This means that at the top of your file you state:
int divisor(int);
int primes(int);
In addition you are forgetting to have primes() return a value. (As this question is tagged as
C++, you might even consider having it return a bool instead of an int).
Also in primes the else clause will now cause a break, but I don't think that's altogether wise. Numbers like 9 will end up in that clause when dividing by 2, but the fact that it is not divisible by 2, doesn't make it prime...
There are also other "smart tricks" when it comes to finding primes, but perhaps that is out of the scope of this question.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am having trouble trying to make a certain for-loop continue to completion in the 1D Queens problem.
First, I used goto statements for everything. Now I am trying to get rid of the goto statements by using functions instead. I will eventually get rid of all of them, but I am focusing on NR (new row) and backtrack first, since they are meant to call each other.
The for loop I am having trouble with is the one that checks if a position is safe for a queen. I point out the for-loop that does not complete in the comments.
//forward declarations
int backtrack (int board[], int& c_position);
//NR: q[c]++;
//if (q[c]==8) goto backtrack;
void NR (int board[], int& c_position) //new row
{
board[c_position]++;
if (board[c_position]==8) {backtrack(board, c_position);}
}
int backtrack (int board[], int& c_position) // backtrack
{
c_position--;
if (c_position==-1) {system("PAUSE"); exit(1);}
NR(board, c_position);
}
int main ()
{
int q[8] = {0}; //1D array, the board, all set to 0;
int c=0;
int count=0;
NC: c++; //new column
if (c==8) goto print;
q[c]=-1;
NR(q, c);
//test to see if position is safe
for (int i=0; i<c; i++) //this is the for loop I am having trouble with
{
if ( (q[i]==q[c]) || ((c-i)==abs(q[c]-q[i])) ) { NR(q, c); }
}
goto NC;
print: //printing the 1D board gives us a single line, where each number represents a row where a queen is
count++;
cout << count << endl;
for(int j = 0; j <= 7; j++)
{
cout << q[j] << " ";
}
cout << endl;
backtrack(q, c);
system("PAUSE"); return 0;
}
You're passing c by reference to a function that passes it to another function that decrements it.
That appears to foil your (outer goto-based) loop's attempt to increment it.
Anyway, that's what I'd look at more closely.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm kind of on the right track, however my output is not quite right. The program asks for the number of integers you have and then it asks for those numbers. For an example is says please enter the number of integers, you can put 3. And then you enter 3 numbers. I can't use arrays because I am a beginner student and we have not learned those yet. Using count is the only way that allows me to input integers. What do I need to add to my program? Again I am a general computer science student so I can't use anything advanced. I used include iostream, namespace int main and all that you just cant see it
int data;
int num;
int count=0;
int max=0;
do
{
cout<<"Enter the number of intergers"<<endl;
cin>>num;
while (count<num)
{
cout<<"Please enter a number"<<endl;
cin>>data;
count++;
if (data<min)
{
min=data;
}
if (data>max)
{
max=data;
}
}
cout<<"Smallest integer:"<<min<<endl;
cout<<"Largest integer:"<<max<<endl;
cout<<"Would you like to continue?"<<endl;
cin>>ans;
} while ((ans=='y')||(ans=='Y'));
return 0;
}
Try out something like this:
int data;
int num;
int max=0, min = 1000000;
cout<<"Enter the number of intergers"<<endl;
cin>>num;
for (int count = 0; count < num; ++count)
{
cout<<"Please enter number #" << count <<endl;
cin>>data;
if (data<min)
{
min = data;
}
if (data>max)
{
max = data;
}
}
cout<<"The smallest number:"<<min<<endl;
cout<<"The largest number:"<<max<<endl;
You can use two temporal variables to store the smallest-so-far and biggest-so-far numbers. On each loop iteration, you check if need to update them.
I don't want to put any code... it is your assignment ;-)
The answer above is the way to do this correct but to be more explicit you may have to update the smallest or the biggest number. For example; give the computer 5 then 4.. Your program prints 4 as a biggest number. However if you update "max" by using any other temporary int, it will give the right number .
First of all, your condition is wrong, it should be while( count < num), since count starts at 0. Now, if you want the quick n' dirty way, simply initialize two variables, min and max to the minimum and maximum values an int can hold. Check the input via comparisons for each variable and update as needed.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have data in a two dimensional array with n rows and p columns.
For example:
vector<vector<int> > dynamicArray(ROWS, vector<int>(COLUMNS));
for(int i = 0;i < dynamicArray.size();++i){
for(int j = 0;j < dynamicArray[i].size();++j){
dynamicArray[i][j] = i*j;
}
}
Now, I want to add several columns to this array. I tried the following (Add a column of all 10s to the array), but if failed:
for(int i=0; i < dynamicArray.size(); i++){
dynamicArray[i].push_back(10);
}
Is there a way to do this?
Thanks!
I've ran your code, and I've successfully added a column. What do you mean by it failed?
Personally I would've flattened the 2 dimensional array into 1 using one single vector.
class DynamicMatrix
{
vector<int> array;
int rows;
int columns;
public:
DynamixMatrix(int r,int c):array(vector<int>(r*c)),rows(r),columns(c){};
int getValue(int x,int y) { return array[x+y*c];}
int setValue(int x,int y, int v) { array[x+y*c] = v;}
void AddRow()
{
rows++;
array.resize(rows*columns);
}
void AddColumn()
{
column++;
array.resize(rows*columns);
}
}