C/C++ :: Write A Function That Accepts Two Arguments / Array Of Integers

Feb 16, 2014

write a function accepts two arguments, an array of integers and a number indicating the number of elements in the array. the function should recursively calculate the sum of all the numbers in the array. Demonatrate the use of the functions in a program that asks the users to enter an array of numbers and prints it sum

i had done this but it wont work

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

[Code].....

View 6 Replies


ADVERTISEMENT

C++ :: Function That Accepts Array Of Integers And Its Size As Arguments

Feb 12, 2014

Write a function that accepts an array of integers and its size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth.

The function should return a pointer to the new array. Use ONLY pointer parameters instead of arrays in both functions; use pointers (not subscripts) to move through elements of both arrays. Before calling the function, display your original array. When the function call is completed, display the new array.

Here is what i got so far:

#include <iostream>
using namespace std;
int *shifted (int * , int);
const int SIZE = 10;
int main () {
int array_size [30];

[Code] ....

View 1 Replies View Related

C++ :: Lambda Accepts No Arguments But It Accesses Increment By Value And Current By Reference

May 24, 2013

In this code:

// Ex10_15.cpp Using lambda expressions
#include <algorithm>
#include <iostream>
#include <iomanip>

[code].....

The lambda accepts no arguments, but it accesses increment by value and current by reference, the latter being used to store the next value to be set. This lambda has the side effect that current will be updated when generate is finished. The following lambda expression is similar, but without the side effect:

[=]()mutable->T{T result(current);
current += increment;
return result;}
"

I dont exactly understand what side affect it is talking about. Wouldn't you want generate to update current? I understand how the second code fixes it though, just takes everything in the enclosing scope by value.

View 2 Replies View Related

C++ :: How To Write A Function That Takes Two Linked List As Input Arguments

Dec 7, 2012

How do I Write a function that takes two linked list as input arguments these linked list contain numbers like this:

num1->3->5->2->NULL (assuming that number 1 is 352)
num2->4->3->9->1->NULL (assuming that number 2 is 4391)

The function should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, returns a 0.

View 3 Replies View Related

C++ :: Write A Function That Receives Two Arrays Of Integers

Feb 19, 2013

There is one question :

Considerint A[10]={ ....................}; // already filled
int B[10]={ ....................}; // already filled

Using PIONTER NOTATION ONLY, write a function that receives two arrays of integers like A and B above. The function should swap the values in A and B. You may NOT use array notation [ ]. Also, you have to use pointers to move among array cells. Note: Both arrays are of the same size, and size should be variable in the function.

View 3 Replies View Related

C++ :: Write A Template That Accepts Argument And Returns Its Absolute Value

Nov 19, 2014

Write a template that accepts an argument and returns its absolute value. The absolute entered by the user, then return the total. The argument sent into the function should be the number of values the function is to read. Test the template in a simple driver program that sends values of various types as arguments and displays the results.

#include <iostream>
using namespace std;
template <class integertemplate>
integertemplate totalint (integertemplate integers) {
cout << "How many integer values do you wish to total? ";
cin >> integers;

[Code] .....

View 8 Replies View Related

C/C++ :: Command Line Arguments With Integers?

Sep 10, 2014

I know this works as expected if your command line arguments are strings.

#include <stdio.h>
int main( int argc, char *argv[] ) {
printf("Program name %s
", argv[0]);
if( argc == 2 ) {
printf("The argument supplied is %s
", argv[1]);

[code]...

Would you be able to do something similar to this if you have integers or would you need to use the first method then convert it with atoi?

#include <stdio.h>
int main( int argc, int *argv[] ) {
printf("Program name %s
", argv[0]);
if( argc == 2 ) {

[code]...

View 4 Replies View Related

C :: Function To Store Command Line Arguments In Array

Feb 1, 2015

In class, we have learned how to set up the main method so we can convert command line arguments to integers and utilize them. Here is an example of a method we learned to store command line arguments as integers in an array:

Code:
int main (int argc, constchar* argv[]) {int i;
int a[argc - 1];
// Fill the array with the arguments on the command line
for (i = 0; i < argc - 1; i++){
a[i] = atoi(argv[i + 1]);

[Code] .....

I understand that there are probably better ways to do it than using the atoi function, but for our purposes, the professor said this was okay.

Now, I am being asked to create a separate function to perform the same task (store integers in an array). I was told I would need to declare the array "out in the main program", and that the function would need to take at least 2 arguments: an array, and an array size.

Firstly, what should my return value be? My professor gave a hint that it would be important for me to figure out what the return value is, but I don't see why it would be anything else than void.

Also, wouldn't I need a third parameter? Perhaps the integers that are being stored in the array? (But how would I represent this as a parameter)?

View 2 Replies View Related

C/C++ :: How To Pass Arguments As Two-dimensional Array Of Typedef To Function

Jul 12, 2012

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 ....

View 3 Replies View Related

C :: Program That Accepts First / Last Name And Displays It Through Another Function

Sep 25, 2014

I was trying to code a program that accepts your first name and then last name and then displays it through another function. I have checked that the assignments are between similar type of variables and pointers, but I don't seem to get it right.

When first input is taken and then second one, the first one's value changes to same as the second. E.G if first name is L and second name is S and after second input both variables, first and sec, become equal to S. Why?

Code:

#include <stdio.h>
#include <stdlib.h>
//FUNCTION PROTOTYPES
char *input(void);
void show(char *f,char *s);

[code]....

View 7 Replies View Related

C++ :: Function That Accepts Integer And Returns A String?

May 5, 2014

How to go about making a function that accepts an integer and returns a string with any one of 5 strings containing the name of the object. For example object number 3 might be "Pen". Object 4 might be "Paper".

To do this would I just use an array?

View 4 Replies View Related

C++ :: Program That Accepts Array Of Characters

Nov 18, 2014

Here's the question: Create a program that accepts an array of characters. And displays the converted array of characters into Upper case if it is given in Lowercase and vice versa. Don't use string, but char.

Example: mychar ="I am A conQUeror."
Code: //My Codes:
#include <iostream>
#include <conio.h>
using namespace std;
int main()

[Code] ....

View 5 Replies View Related

C++ :: Write Function That Takes Array And Returns True If All Elements In Array Are Positive

Jan 21, 2013

Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.

View 6 Replies View Related

C :: Program To Write Even And Odd Integers Into Different Files

Sep 7, 2014

-Create three files namely all_numbers, odd_number and even_number.
-Get the number of records and the numbers to store from the user.
-Save all the numbers in "all_numbers" file.
-Read each number and check whether it is odd or even.
-Store the odd numbers in odd_number file and even numbers in even_number file.
-Finally, print the numbers stored in odd_number and even_number files.

The output of the program will look like this.

How many records that you want to store :41

2
3
4

The data are written too the respective files.

The even numbers are 2 4
The odd numbers are 1 3

View 9 Replies View Related

C++ ::  Write A Program That Prints Out The Even Integers Between 2 And 100

Nov 4, 2013

#include <iostream>
using namespace std;
int main()
{
int num = 2;
int count = 0;
while (num <= 100)

[Code]...

The code works fine but I need the results to print out like

2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40

instead of

2
4
6
8
10
12

I don't know how to do this.

View 7 Replies View Related

Visual C++ :: Write A Function With Array And Int Parameter?

Nov 25, 2012

i need to write a function with an array parameter and an int parameter.

that array has to be filled with first 10 prime numbers that are exact or higher than the int parameter...and then i need an average value of those 10 prime numbers...

The problem is im not really sure how i should do the part to fill the array with prime numbers that are higher than that int??

Code:

int avgprimearray (int higharray[], int somenumber){
}

View 1 Replies View Related

C++ :: Write A Function That Return Maximum Value Of Array Of Doubles?

Oct 9, 2014

I'm trying to get correct answers from my exam to study for a midterm:

Write a function that returns the maximum value of an array of doubles.

Declaration:

double maximum(array[],int a_size);

So far I've done:

double maximum(array[], int a_size) {
double maxVal;
maxVal = array[0];
for(int i=1; i < size; i++) {
if(array[i] > max
maxVal=array[i]
}
return maxVal;
}

I just need any corrections.

Second, I need to write a function definition that merges two vectors containing integers, alternating elements from both vectors.

Prototype:

vector<int> merge(const vector<int> a, const vector<int> b);

This is my definition so far:

vector<int>merge(const vector<int> a, const vector<int> b){
vector<int> merged;
for(int i=0; i<a.size+b.size; i++) {
if(flag == true)
merged.push_back(a);
flag = false;
else
merged.push_back(b)
flag = true;}

I think I'm lost in the second part...

View 2 Replies View Related

C :: Write A Function That Finds Integer In Array And Returns Its Corresponding Index

Mar 20, 2013

I have an assignment which requires me to do the following:

Required to write a function that finds an integer in an array and returns its corresponding index. The function must be called findNumber. It must have FOUR parameters:

- The first parameter is the array to be searched
- The second parameter is the integer to be found within the array
- The third parameter is the size of the array
- The fourth parameter is an integer that indicates whether the array is sorted. A value of 1 means the array is sorted; a value of zero means the array is not sorted.

Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.This is my code : (It compiles perfectly but it does not produce any outputs)

Code:

#include <stdio.h>
#define SIZE 10
size_t findNumber(int *sort, const int array[],int key,size_t size);
int main(void){
int a[SIZE];
size_t x;

[code].....

View 1 Replies View Related

C :: Create Function To Create A Dynamic Array That Fill With Randomly Generated Integers From 0 To 50

Oct 26, 2013

I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.

Code:

* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}

[code]....

View 7 Replies View Related

C :: Radix Sort Function To Sort Array Of 64 Bit Unsigned Integers

Aug 19, 2013

Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.

Code:
typedef unsigned long long UI64;
typedef unsigned long long *PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) {
size_t mIndex[8][256] = {0};
/* index matrix */
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;

[Code]....

View 9 Replies View Related

C++ :: Accept Integer Array And Its Size As Arguments And Assign Elements Into 2 Dimensional Array

Jan 10, 2015

Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is

1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0

View 1 Replies View Related

C++ :: Function Does Not Take 3 Arguments

Apr 27, 2013

I am doing a problem where I need to use arrays of string objects that hold 5 student names, an array of one character to hold the five students' letter grades and five arrays of doubles to hold each student's set of test scores and average score.

When I try to run it, I get these five errors.

error C2660: 'getTestScore' : function does not take 3 arguments : line 39
error C2660: 'getTestScore' : function does not take 3 arguments : line 45
error C2660: 'getTestScore' : function does not take 3 arguments : line 51
error C2660: 'getTestScore' : function does not take 3 arguments : line 57
error C2660: 'getTestScore' : function does not take 3 arguments : line 63

what is wrong.

Here's my code.

View 4 Replies View Related

C :: Arguments When Declaring A Function?

Jun 5, 2013

I am a bit confused about how specific one must be with arguments when declaring a function. I'll show you two functions from the book I'm using to learn C to show you what I mean.

Example 1 (greatest common denominator function):

Code:
void gcd (int u, int v) {
int temp;
printf ( "

[Code] ....

So in that function, there are exactly two arguments, because that's how many arguments the algorithm to find the gcd takes. No problem there, makes sense to me. Then further in the chapter on functions I run into this,

Example 2 (square root function):

Code:
float absoluteValue (float x) {
if ( x < 0 )
x = -x;
return x;

[Code] ....

In this second example, we have a square root function that is preceded by an absolute value function. The absolute value function has the one argument, "float x", however when this function is called within the square root function, the arguments "guess * guess * -x" are passed to it. I'm confused how this absolute value function is working with all of that inside it, when it was originally declared with just "x." The only possibility I can think of is that this expression is treated as a single unit, but I'm not sure.

View 2 Replies View Related

C :: Function With Multiple Arguments

Jan 15, 2015

I am actually developing an nginx module in C.I am not to bad in C, but i got a big problem to pass argument to a vadiadic function.This function look like the well good old printf, but you put a buffer as first argument, the last address to stop to put data as second argument (in my case it is the last adress of disponible memory), a string that look like one in printf, an the other argument after.Here is the problem, the 4th last argument does not have the good value. In fact, It seem to be random value from memory. I Use gcc (Debian 4.9.1-19) 4.9.1.

Code:

/* ngx_html_log.h */
#ifndef NGX_HTML_LOG_H
#define NGX_HTML_LOG_H
#include <ngx_vasm.h>
}

[code]...

View 3 Replies View Related

C++ :: Function With Template Arguments

May 26, 2013

I'm trying to create a callback wrapper for pointers to member functions of any class by using templates, std::function and std::bind. This will be used to send incoming sf::Event's to classes who register callbacks with an event manager. I based my code off of the example on this page: URL.....Here's what I have:

class EventCallback
{
std::function<bool(const sf::Event&)> func;

public:
template<typename T>
EventCallback(T* object, bool(T::*func)(const sf::Event&)) { func = std::bind(func, object, std::placeholders::_1); }
bool run(const sf::Event& evt) { return func(evt); }
};

[code]....

View 4 Replies View Related

C/C++ :: Pointer As Function Arguments

Dec 26, 2014

How this code work bcoz when pointer variable assigned in called function and how different values get as resultant output, ans 2 97 for below code. How the code wil execute so that i can validate ans

#include <stdio.h>
int main() {
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);

[Code] ....

View 6 Replies View Related







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