C++ :: How To Pass Array Into If Statement
Mar 16, 2014#include <iostream>
#include <iomanip>
using namespace std;
double calculateCharges(double hours[], double sizeofArray);//call function
[Code]....
#include <iostream>
#include <iomanip>
using namespace std;
double calculateCharges(double hours[], double sizeofArray);//call function
[Code]....
I want to pass an array to a function and change the array, how can I do that?
Code:
#include<stdio.h>
/*function to change the array*/
void rearng(int *qw) {
int i,j,a;
int sa[8]; //temp array
int c = 0;
int high;
[Code] ....
I am trying to pass an array of objects however i m finding some problems.
pieces.h
Code: class Cpiece{
public:
Cpiece* board[8][8];// array of objects
virtual char getcolor() =0;
virtual char setcolor(char colour)=0;
[Code] .....
I need to pass an array of 10 instances of a custom class to a function. The snippets of code are posted below. How would I do this right?
The prototype:
Code:
int Search(Vertex vertex[], ofstream &outfile);
The implementation in the main function.
Code:
Search(vertex[10], outfile);
I'm trying to pass an array from one function to another without resorting to pointers and I have to use Pass by Reference. I can't seem to figure it out. I've been looking at many examples and just don't get it.
the function createAbc makes an array populated with the Alphabet. I need to pass that into my display function so that i can work with the array there. I don't know how to pass it in there... :(
#include "stdafx.h"
#include <iostream>
#include <cctype>
[Code]...
I am trying to write a poker program. I created a class to create a deck of cards..and then shuffle the deck..Now I am trying to pass value from the deck to an array to deal a hand..so 5 cards to player1[] array.. I tried doing it directly such as.
for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}
But I would get error such as [Error] no match for 'operator=' in 'player1[j] = deck[j].Card::toString()'
So then I tried using a pointer..
char *getCard;
getCard = new char;
for (int j = 0; j < 5; j++) {
getCard=deck[j].toString();
}
but I would get this error [Error] void value not ignored as it ought to be. So how can I pass a value from the object deck..to an array? or should I be doing it another way?..I tried to think of a way to do it via a function but really got hung up there..
here is full code
#include <stdlib.h>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
const char* FaceString[13] = {"Ace", "2", "3", "4",
"5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"};
[Code] ....
I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage. Here's a simplified version of what I'm working on:
#include <iostream>
class board {
public:
int state[64];
board(int arr[])
[Code] ....
Why this doesn't work and how to properly pass an array? Notes: I don't want to copy the array. Also, I'm using MSVC++ 2010 Express.
I've been given an assignment with the below questions.
1. What is the difference between pass by reference & pass by pointers?
2. What is the use of the initialization list in the constructor?
3. What is meant by a reference & its advantage?
4. Class has a reference, pointer and a const. Is it possible to write the copy constructor & assignment operator overloading funciton? how? ( Since reference is there, I'm not sure on how to write for it)
5. Example for a variable decleration and definition? (I know for function but for variable don kw how)
6. static and const static what is the difference??
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){
[Code]...
I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1
The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
[Code]....
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
If a function takes N-dimensional array as a parameter, what is generic way to do that? I couldn't figure out a nice way to do that.
View 7 Replies View Relatedhow would i got about making an if statement for an array so it prints what u enter
Code:
example:
struct input{
float peopleseen;
float creditrate;
char insurprov[12];
[Code] ....
so I am trying to have an switch statement array. but every time i complete a case statement the whole process is returned. the thing is i want the menu to stay up even if i complete the case.
View 2 Replies View RelatedI am attempting to write a program " that has a function that is passed an array of int and its size, and with that it prints out only the numbers that are above average. "
I have included my code so far, but I am only getting one value as output, and that value seems to be completely off. I keep trying, but I am really stuck.
#include <iostream>
using namespace std;
int average(int values[],int size);
int main(){
int size;
int values[] = {1,2,3,4,5,6};
[Code] ....
Added the floats in the average() function. But there is still a value problem.
How to pass my array to the function and return it as a sorted vector. I'm kind of lost with the functions part.
Problem: Write a program that performs a sorting routine as discussed in class. Create an array of 10 integers (e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87). Pass this array to a separate function that sorts the integers and returns a vector containing the sorted integers. Use either the SELECTIONSORT algorithm or the INSERTIONSORT.
Code so far:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
[Code]...
Looking for info about the typedef 2-dimensional array ....
Suppose I have typedef two_Darr[3][3];
How to declare this and how to pass it to other function definition in the main function ....
I have this code:
const BYTE original[2][4] = {
{0x00, 0x00, 0x00, 0x00},
{0xFF, 0xFF, 0xFF, 0xFF}
};
void function(const BYTE** values){
[Code] ....
You might notice that the above code doesn't compile, this is the error:
cannot convert parameter 2 from 'BYTE [2][4]' to 'BYTE *'
1>
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Even after some search I couldn't really find an answer to my problem, how do I pass the const BYTE array which I declared above in the function as a parameter (or what structure do I need to set for the function as a parameter)?
// explain difference between pass by value and pass by reference
void addOne(int i)
{
i++;
}
void addOne(int& i)
{
i++;
}
The issue that I'm having is that when I run this part of the code it seems to enter the if statement and set it to 0 when comparing the array to the input.
What I want it to do is to search the array for the grade, set it to zero, and then exit the loop in case there are any repeat grades. It always sets the first number to 0 and then exits.
//Below is the prototyping for the array
int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};
//There is some code between here, but it is probably not relevant so I didn't post it.
printf("Enter the grade that you want to remove. Press Enter after each input.");
printf("Enter 0 to exit: ");
[Code] ....
I didn't mean to leave the "prototyping" part in the code. I was initially going to put the function prototypes in there, but decided it wasn't necessary for the question.
Just making typical console game where hero moves with w a s d.
However, in my code, while a and w work (minusing), s and d dont work (adding). I have used a switch statement to change the char position of the hero based on kbhit.When you hit s or d, the buttons that add 1 to the 2nd dimension of the array, the screen goes crazy!
#include <iostream>
#include <string>
#include <conio.h>
#include <ctime>
#include <cstdlib>
using namespace std;
const char PLAYER = 'H';
[Code] .....
In my project, GreedyKnap::calKnap(int nItem, int nCapacity, float fWeights, float fProfits);
This function include two array member pass as parameter. how can i do this?
I have an if statement that should either match a text variable that the user has entered and a another text variable that has been got from an array but they won't match even if they are the same,Im at a lost with it.
void displayQuestion(int noQuestion, string questionArray[], string answerarray[], string answer, double scorearray, double finalscore){
cout << questionArray[noQuestion] << endl;
cout << "enter an answer " << endl;
cin >> answer;
[Code] ....
How to make if else code below into SWITCH STATEMENT?
cout << "Enter the temperature: ";
cin >> temp;
cout << "Enter the pressure: ";
cin >> pressure;
cout <<endl;
[Code]....
i think i understand both concept. i know that pass by value is that the function receiving those values makes actually a copy of those parameters. Passing by reference makes the variable in the main function to actually see those changes in the other function, instead of a copy, and apply them to the variable in the main function. my question is, why would i pass it by reference if i just can make a return type function.
View 5 Replies View RelatedI need to put my Dice() function as pass by reference and something as a pass by value, it can be in the same function.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
[Code].....