C/C++ :: Functions Passing Size Of Arrays As Value Parameters?

Mar 11, 2014

we were given this code:

// Music Shuffle Program
// This program takes an array of strings and randomly permutes their order.
// This allows us to generate new song shuffles.
#include <iostream>

[Code]....

or are they referring to something else?

View 4 Replies


ADVERTISEMENT

C :: Recursive Functions With Arrays As Parameters

Sep 21, 2013

I wrote a fuction in C with the prototype 'void raisePowerOf2(int array[],int pow);'

If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)

But it seems that this function doesn't give the exact answer

And I tried this from java also,with the same implementation.It produced the answer precisely .

I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly

This is the code in c

Code:

#include<stdio.h>
void raisePowerOf2(int array[],int pow);
int main(){
int a[2]={1,-1};
raisePowerOf2(a,5);
return 0; }
void raisePowerOf2(int array[],int pow){

[Code]...

This is the code in java....

Code:

public class NewClass4 {
void raisePowerOf2(int array[],int pow){
final int len=array.length;
int store[]=new int[len+1];
int qtnt=0;
for(int i=len-1;i>=0;i--){
store[i+1]=(array[i]*2)%10+qtnt;
qtnt=(array[i]*2)/10;

[Code]...

View 7 Replies View Related

C/C++ :: Recursive Functions With Arrays As Parameters

Sep 21, 2013

I wrote a fuction in C with the prototype

'void raisePowerOf2(int array[],int pow);'

If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)

But it seems that this function doesn't give the exact answer

And I tried this from java also,with the same implementation.It produced the answer precisely .

I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly .

This is the code in c

#include<stdio.h>
void raisePowerOf2(int array[],int pow);
int main(){
    int a[2]={1,-1};
    raisePowerOf2(a,5);  
    return 0;
}  void raisePowerOf2(int array[],int pow){

[Code] ....

View 5 Replies View Related

C :: Passing Arrays Into Functions

Nov 4, 2013

i have a program that works, but now I am trying to get function1() to work. What it has to do is bring in the array and populate it with random letters. I don't know much about functions.

Code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxrow 20 //defines maxrow as a constant of 20
#define maxcol 30 //defines maxcol as a constant of 30
}

[code]....

View 2 Replies View Related

C++ :: Passing Structure Arrays To Functions?

Oct 31, 2013

Description: Use functions and structures to simulate storage in a warehouse

*/
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
struct Bin {std::string name; int Quantity;}; //create a structure for "Bin"

[code].....

I keep getting a linker error on every function. what am I doing wrong?

View 2 Replies View Related

C++ :: Passing Char Arrays Between Functions Of Same Class

Nov 16, 2013

I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.

Here's my code:

Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph

[Code] ....

View 4 Replies View Related

C++ :: Passing / Returning Character And Integer Arrays With Functions

Jun 21, 2013

Passing and returning character and integer arrays with functions using simple programs...use pointers if necessary

View 1 Replies View Related

C++ :: Passing Parameters To A Thread

May 9, 2013

I've been using threading for a while, that was no needing parameters. so I used for example the following line :

_beginthread(functionName, 0, (void*)10);

and it worked great...

but now I have a function that has an int parameter (int num), and I dont know how to transfer it using _beginthread...

View 2 Replies View Related

C++ :: Passing Classes As Parameters?

Oct 14, 2013

how would I pass this parameter and how/why is it not working this way? I've tried many different methods to this and I can't quite seem to figure it out.

studentList student;
student.push(252875, "Jerry", "UTPA");

What I thought would work

class student {
public:
int id; //student ID number
string name; //student’s name
string university; //student’ university
};
//student list is a doubly linked list of students.

[code]....

My header file.

I am honestly not sure where to start here. I would assume that it would know what to do with the varibles but it doesn't seem to want to accept them. It gives me

Error1error C2660: 'studentList::push' : function does not take 3 arguments

2IntelliSense: no suitable constructor exists to convert from "int" to "student"

View 11 Replies View Related

C++ :: Setting Up Counters - Using Function / Passing Parameters

Jun 30, 2012

Need setting up counters for this program which should

Given a file of text, assume that
a "word" is 1 or more consecutive, non-whitespace characters
a "sentence" is a series of words terminated by either a period, exclamation point, or question mark

Design a C++ program (using functions/passing parameters) that will

-interactively prompt for and read the name of an input file
-interactively prompt for and read a string to search for
-open the input file (using an input filestream variable) and with one pass through the file
-count the number of "words" in the file
-for each word, make sure all letters, except the first, are lower case - leave the first character unchanged
-count the number of "sentences" in the file
-count the number of letters in the file (A-Z,a-z)
-count the number of consonants in the file (consonants are letters that are not vowels - the vowels are: a, e, i, o, u, A, E, I, O, and U)
-count the number of nonwhitespace characters in the file that are NOT letters
-count the number of times the search string is found in the file (must be an exact match) - search for matches AFTER upper case letters have been coverted to lower case

View 2 Replies View Related

Visual C++ :: Passing As Parameters To A Function Characters Coded In UTF 8

Apr 9, 2014

I'm using the Visual C++ Express 2008 and i need to pass as parameters to a function characters coded in UTF 8. My environment is Windows 7. The editor of the VC++ write in UTF 8 or UTF 16? If it writes in UTF 16 how can i change it?

View 2 Replies View Related

C :: How Arguments Handled If Passing Parameters Are Different Types Compared To Declaration

Jun 12, 2014

Suppose if i write a test program like

Code:
void function1(unsigned int var1);
int main(void) {
function1(-3);
}
void function1(unsigned int var1) {
printf("%d", var1);
}

The output is -3. how it happens the argument is unsigned but iam passing signed but still prints the signed value. My bigger question is how the arguments are handled if the passing parameters are different types compared to declaration.

View 2 Replies View Related

C++ :: Optional / Default Parameters In Class Functions?

Aug 19, 2014

In the thread "Making a argument optional to function", it is stated that to set default values for arguments of a function you can simply do so in the function definition, like:

int myfunc(int a, int b, int c=3) {...}

This then automatically puts c to 3 in the function body if a call like myfunc(1,2); is made, if I understood correctly. However, this does not seem to hold for class functions. For example, something like:

class classy {
public:
int class_func(int, int, int); // class function prototype
}
int classy::class_func(int a, int b, int c=3) {...}

fails to compile. What I would like is to be able to call class_func outside of this class (by including it as a header in another macro), optionally specifying c. If c is not specified in the call, it should use a default value.

View 2 Replies View Related

C++ :: Class Member Functions With Pointer Parameters?

Jan 30, 2013

Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!

class Album {
private:
char * artist; // band or singer’s name
char * title; // title of the album

[code]....

The input will be an array. My questions: First, am I on the right track?

When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?

also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?

what is the difference between *& and * as parameters?

View 5 Replies View Related

C++ :: Functions Without Parameters - Compute And Print Total Number Of Products For Company

Mar 27, 2013

Summary: 6 companies have a product in 5 different warehouses. Each company is identified by a positive ID number and each warehouse is identified by a number (1 for the first, 2 for the second,…)

Object: the object of this assignment is to write a C++ program which asks the user to enter a company ID number, and the number of products in each of the warehouses. It then computes and prints the total number of products for that company

If the total number of product is less than 100, it prints the message “place a new order”

Input: for each company, its ID, and the number of products in each warehouse with appropriate prompt messages.
Example: Enter company ID number: 101
Enter number of products in warehouse #1:30
Enter number of products in warehouse #2:60
Enter number of products in warehouse #3:0
Enter number of products in warehouse #4:5
Enter number of products in warehouse #5:27
The total for company 101 is:122

Output: for each company, the total number of product, and the message “place a new order” if the total number of product is less than 100.

Method:
1. Define global variable, int total_prod to hold the total number of products for a company
2. define the function void compute_total() that uses a loop to read the number of the products in all warehouses for one company, computer the total number of products and store it into the global variable total_prod.
3. Define the function void new_order() that determines if a new order need to be placed as follows: if the total number of products (in the global variable total_prod) is less than 100, it prints the message “place a new order”
4. Your function main does the following in a loop:
- read a company ID number
- call function compute_total() to read the number of the product in all warehouses for that company, and to compute their sum
- print the total number of the product for that company with an appropriate message
- call the function new_order() to determine if a new order need to be placed.

View 17 Replies View Related

C :: Passing Into Functions

Feb 18, 2013

forget everything from before. It came out of confusion regard the supplier functions. dis_s() and read_s() the part functions work and are not any different really.

Code:

#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MPS 10 //Max Part Size
#define MSS 10 //Max Supplier Size
}

[code]....

when I run the dis_s() function it just prints out garbage until it segments. starts with a bunch of 0 and newlines until it starts printing locations on my computer...... it worked on campus with debian and I only copied it from gmail to my computer so I really don't understand whats up(im on mint).

View 7 Replies View Related

C++ :: Passing Vector To A Function - Getting Negative Length And Size

May 22, 2013

I am writing a raytracer, and currently I'm working on creating a bounding volume hierarchy to accelerate the process. To do this, I am first creating a vector that holds each of the objects in the scene, and passing this vector to the constructor for my BVH.

Code:
//in header
BVH_Node* bvh;
//in main raytrace function

[Code] .....

I am testing a scene that has only 2 objects, and so it goes to the size == 2 check. The first time it hits makeLeaf(), I segfault. I've used both gdb and valgrind, and of course it's a memory mapping error. gdb's backtrace tells me that the length of the vector I've passed in is -805305610 and the capacity is -21, and that it is inside my makeLeaf() function that the error occurs.

Here's the function:

Code:
BVH_Node* BVH_Node::makeLeaf(GeomObj* v){
BVH_Node* node;
node->obj = v;
node->isObj = true;
return node;
}

The segfault happens at
Code: node->obj = v;
If I run my raytracer without a BVH, the objList works perfectly.

View 8 Replies View Related

C :: Passing Arguments To Functions

Feb 4, 2013

Having a little trouble passing arguments to functions.

I wrote this simple program to get the hang of it but I'm quite stuck. I'm sure you will be able to get at what i want the program to do...

Code:
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <windows.h>

void menu(int HP,int Gold,int Armour);

[Code] ....

View 3 Replies View Related

C :: Passing Pointers To Functions

Feb 28, 2015

I have been struggling with pointers. I am trying to write a program that first asks a user to input a filename. It then checks if the file exists and if it does it passes a pointer to the next function. The next function then asks the user for a specific word to look for and the function will search a text file for the word and do some other operations. My problem is that I do not understand how to use the pointer returned by my first function as an input to another function.

The following code has the first function file_check() and the second function word_search() which I think the way I am declaring it is the problem.

Code:
FILE *file_check();
void word_search(FILE *);
int
main(void) {
FILE* check= file_check();
// word_search(check);

[Code] ......

View 6 Replies View Related

C :: Passing Variables Among Functions

Mar 6, 2015

I have this code where I am trying to retrieve the contents of the variable dev1 and dev2. for some reason when i compile and run I am getting 0 and 0.

Code:

typedef struct {
uint32_t x;
uint32_t y;
} sample;
void get_sample(sample *one)

[Code].....

View 9 Replies View Related

C++ :: Passing Structs And Functions All In Same

Mar 8, 2013

I think i am getting confused with passing structs and functions all in the same...When I run through the program (it compiles), the functions that add coins do not add, but rather just replace an old value with a new one.

#include <cstdlib>
#include <iostream>
using namespace std;
struct coinbox {

[code].....

View 1 Replies View Related

C++ :: Passing Values To Functions?

Jul 29, 2013

I am trying to get some confirmation about how to pass to functions. If you want to assign default values to certain parameters, and have others defined inside the body of int main(), then the parameters which will have default values go at the end of the list. Is that correct?

i.e. The following code is wrong, because we cannot leave a black in the function call on the third line of the main function. However, if we switch the prototype to void Passing (int a, int c, int b = 1); and the function definition to void Passing (int a , int c, int b) everything will be okay and we can call the function as Passing (a, c).

In brief, we cannot do this EVER:
Passing( a, , c)right?
#include <iostream>
using namespace std;

[Code]......

View 4 Replies View Related

C++ :: Passing Types To Functions

Jan 12, 2013

I've got a game engine with a line-trace collision method which returns the first object it hits. I'd like to be able to pass it a class-type so that it can ignore objects of other types.

consider this pseudo-code:

Entity* TraceEntity( const Vec3f& LineStart, const Vec3f& LineEnd, const Type atype ) {
// check collision on entities, ignore entities of type 'atype'.
// return whatever it finds
}

I'd like to do this without template classes because it will result in a significant bloat in executable size every time I decided to trace for a new entity type (I've really developed a distaste for templates for this reason)

using type_info only checks for an object's deepest subclass, so it won't work for class C : public B : public A if I'm looking for classes of type B.

View 5 Replies View Related

C++ :: Passing Variables Between Functions

Jan 29, 2013

I need passing some variables between functions. Here is the first part of the code which does work.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct CarType {
string maker;

[Code] ....

Now the book says to take the following program and add a member function to the CarType class which prints the values of all of its data members. Add two more data members which are relevant for cars. Add the use of these data members to the program (to the assignment statements for MyCar, to the operator prompt and input inside the getYourCar function, and to the print function you have created).

Here is my code. Whenever I run it, it takes my assigned variables in MyCar and prints those instead of the one which the user is inputting.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
using namespace std;
struct CarType {
string maker;

[Code] .....

View 3 Replies View Related

C++ :: Passing Ifstream Object Between Functions

Mar 29, 2013

Code:
void lexer(ifstream& inputfile) {
string line;
getline(inputfile,line);

[Code] ......

I am trying to pass input file between two functions. The code compiles but immediately upon running the program, there is a "bad cast" run time error.

View 9 Replies View Related

C++ :: Grade Average Passing In Functions?

Sep 3, 2013

My average is failing but I played and played with it and I still keep crashing.

Code: #include <iostream>
using namespace std;
void getScore(int score[], int NumGrades)
{
cout<<"How many grades do you need to enter?"<<endl;
cin >> NumGrades;
cout<<"Enter the Students Grade(s)"<<endl;
for (int i=0; i<NumGrades; i++) {
cin >> score[i];

[code].....

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved