473,804 Members | 3,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically allocated arrays

hi all, im super new with c++ (and no background in programming
whatsoever!)
i have a task to make functions to calculate total profit and the
average number of things sold. data is read from a file. my problem is
with functions, namely setting up parameters and later calling
functions in the main().

here are the two functions (?) that i wrote so far, one is supposed to
get total profit and the other calculate average

#include <iostream>
#include <fstream>
#include <vector>
using std::ifstream;
using namespace std;

double getTotalProfit (double totalProfitOrde r, float
profitLossForFr eeCookies ) { // what do I set as parameters in this
function?
int freeCookies = 5;
float cookieProductio nCost = 0.10;
float profitLossForFr eeCookies = freeCookies * cookieProductio nCost;
double totalProfitPerO rder = cookiesSoldPerO rder * cookiePrice -
profitLossForFr eeCookies;
return totalProfitOrde r;
}
double avgNumOfCookies Sold (double avgOfCookieSale s, int
cookieOrderCoun t) {
double avgOfCookieSale s;
int totalCookiesOrd ered = 0;
int cookieOrderCoun t = 0;
cookieOrderCoun t ++;
inData >cookiesSoldPer Order;
totalCookiesOrd ered += CookiesSoldPerO rder;
avgOfCookieSale s = totalCookiesOrd ered/cookieOrderCoun t;
return avgOfCookieSale s;
}

i realize my parameters are screwed up, but if you could show me what
i am doing wrong i'd appreciate it.

(for the average i plan on using vectors and dynamically allocated
arrays, but i have trouble setting those up as well - its a mess
really!, however, the setting up functions and calling them is giving
me the most trouble)

any help is greatly appreciated!

Apr 15 '07 #1
1 1359
Sn*******@gmail .com skrev:
hi all, im super new with c++ (and no background in programming
whatsoever!)
i have a task to make functions to calculate total profit and the
average number of things sold. data is read from a file. my problem is
with functions, namely setting up parameters and later calling
functions in the main().

here are the two functions (?) that i wrote so far, one is supposed to
get total profit and the other calculate average

#include <iostream>
#include <fstream>
#include <vector>
using std::ifstream;
using namespace std;

double getTotalProfit (double totalProfitOrde r, float
profitLossForFr eeCookies ) { // what do I set as parameters in this
function?
int freeCookies = 5;
float cookieProductio nCost = 0.10;
float profitLossForFr eeCookies = freeCookies * cookieProductio nCost;
profitLossForFr eeCookies = freeCookies * cookieProductio nCost;
double totalProfitPerO rder = cookiesSoldPerO rder * cookiePrice -
profitLossForFr eeCookies;
totalProfitPerO rder = cookiesSoldPerO rder * cookiePrice -
profitLossForFr eeCookies;
return totalProfitOrde r;
}
double avgNumOfCookies Sold (double avgOfCookieSale s, int
cookieOrderCoun t) {
double avgOfCookieSale s;
^^Redeclaration of parameter. Remove.
int totalCookiesOrd ered = 0;
int cookieOrderCoun t = 0;
^^Redeclaration of parameter. Remove.
cookieOrderCoun t ++;
inData >cookiesSoldPer Order;
totalCookiesOrd ered += CookiesSoldPerO rder;
avgOfCookieSale s = totalCookiesOrd ered/cookieOrderCoun t;
return avgOfCookieSale s;
}

i realize my parameters are screwed up, but if you could show me what
i am doing wrong i'd appreciate it.
--
OU
Apr 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3972
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) { int* pInts = new int; for(int i = 0; i < 100; i++) vec.push_back(pInts);
10
2599
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr = malloc(sizeof(*arr) * 4); /* I want to dynamically allocate
5
5601
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
37
2793
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
5
1821
by: tricard | last post by:
Good day all, I have created a two dimensional array (matrix for my purposes) whose size is dynamically allocated. (i.e. rowSize and colSize are both taken as input, then malloc() is used to dynamically allocate the required memory). After the matrix is returned to main I want to pass it to a function, printMatrix() and have it displayed on screen. However, I do not want to send the rowSize and colSize arguments; instead I want to have...
94
4788
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
2
3314
by: flyvholm | last post by:
According to a couple of other threads you can't use sizeof with dynamic arrays - you'll have to keep track of the memory allocated. In my case, strings are filled into a dynamic array by a database query (postgresql), meaning that it is not readily possible to keep track (a preprocessor converts the database query to C code, using some functions that in turn do the memory allocation). I want to hand the array to a function that needs to make...
6
2631
by: bwaichu | last post by:
Is my understanding of the allocation of these correct? I used fixed sized allocations for the example below, so I realize there is some optimization that can be done to those. I would like to use these in a linked list for something else I am working on, but I want to make sure my understanding of the concept is correct. For example, from the code below string would equal 'h' after
7
8733
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a two-dimensional array, like this: int x; but I want it dynamically-allocated, and I want expressions that refer
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.