Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 04-25-08   #1 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default C++ array packing...

Alright, so here is what i have going...

list[50];

i want to ask the user what array element they want to delete. i am thinking something like this.

cin << i;

for (i >=0 ; i <=50 ; i++)
{
list[i +1 ] = list[i]
}

And thats all i have going... cant think of how to do this.

The idea is that i have a static array of 50 elements. Then ask the user what element they want to delete. I am not using any type of "new" or "delete" commands, so what i need to do is just over write that array element that needs to be deleted, by the element to its right... And then stack the array by moving over all the other elements over from the right of the deleted element, to the left...

I should know this on. I am having a huge brain fart on this one...

HELP!!!!
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #2 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,268

Rep: 280 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 215
Folding Team Rank: 274
Hardware Reviews: 1
Trader Rating: 3
Default

Close, you have the right idea, but with the way your code is now you would get a couple of syntax errors and move everything to the left, eventually deleting element 50.

Here, this should work:

Code:
cin >> i;

for (i; i == 50; ++i)       //for starts at i, increases until i == 50                    
{                          //and saying ++i is more efficient than i++ in most cases
    list[i] = list[i + 1]  //The ith element gets the value from i+1, thus shifting everything to the left.
}
Now the value in element 50 is the same as in element 49, so you may want another little snipit of code after the for loop setting list[50] to the default value, whatever you choose it to be.
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 04-25-08   #3 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default

well, i do want to keep the list[50] at 50 elements. So after this loop runs, could i just say something like:

null = list[50];

???

from my understanding, and the fact that what i am writing this for isn't using pointers, we are passing each array element by its value, and not by reference. With this said, when i move the value of list[50] into list[49], what is inside list[50]?? nothing? null?? the same as list[49] after i copied the value from list[50]??
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #4 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default

Alright, now im lost... I thought i could figure it out once i had the loop for the array issue, now im all mest up...

I have this app running, minus the 'deleteFoodIndex()' and the 'calculateMeal' functions...

The deleteFoodIndex just identifies the element that the user wants to delete, and moves over all the elements from its right, over one place, essentially moves over the elements over the element that the user wants to remove...

The calculateMeal function needs to do the following:

asks the user what muliple foods they want to add together, then take those foods identified by the user, and add up each elements' properties together...

sample.

add food: Chicken
food type: 3
calories: 32
carbohydrates: 52
fat: 45
cholesterol: 69
sodium: 12
protein: 56

Add another food...

add food: steak
food type: 0
calories: 56
carbohydrates: 48
fat: 17
cholesterol: 48
sodium: 56
protein: 89

So now we have two foods in the array. list[0] is the chicken, and list[1] is the steak...

How do i add up each of the calories, carbohydrates,fat, cholesterol, sodium, protein, from each food??

--------------------
Foods placed in meal: chicken, steak

combined properties

calories: 88
carbohydrates: 100
fat: 62
cholesterol: 117
sodium: 68
protein: 145


The function needs to add up all the user entered foods from the meal function, not from the add function, and add up these things...

not sure how to do this, and for this assignment, i cant use pointers!! Damn it!!!
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #5 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,897
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

Quote:
Originally Posted by The Bartender Paradox View Post
Close, you have the right idea, but with the way your code is now you would get a couple of syntax errors and move everything to the left, eventually deleting element 50.

Here, this should work:

Code:
cin >> i;

for (i; i == 50; ++i)       //for starts at i, increases until i == 50                    
{                          //and saying ++i is more efficient than i++ in most cases
    list[i] = list[i + 1]  //The ith element gets the value from i+1, thus shifting everything to the left.
}
Now the value in element 50 is the same as in element 49, so you may want another little snipit of code after the for loop setting list[50] to the default value, whatever you choose it to be.
If the loop condition is "i==50" the loop only iterates while i==50 (i.e. it exits immediately)

he wants

Code:
cin>>i;
while(i<49)
  list[i]=list[++i];
list[i]=NULL;
Quote:
Originally Posted by JoBlo69 View Post
Alright, now im lost... I thought i could figure it out once i had the loop for the array issue, now im all mest up...

I have this app running, minus the 'deleteFoodIndex()' and the 'calculateMeal' functions...

The deleteFoodIndex just identifies the element that the user wants to delete, and moves over all the elements from its right, over one place, essentially moves over the elements over the element that the user wants to remove...

The calculateMeal function needs to do the following:

asks the user what muliple foods they want to add together, then take those foods identified by the user, and add up each elements' properties together...

sample.

add food: Chicken
food type: 3
calories: 32
carbohydrates: 52
fat: 45
cholesterol: 69
sodium: 12
protein: 56

Add another food...

add food: steak
food type: 0
calories: 56
carbohydrates: 48
fat: 17
cholesterol: 48
sodium: 56
protein: 89

So now we have two foods in the array. list[0] is the chicken, and list[1] is the steak...

How do i add up each of the calories, carbohydrates,fat, cholesterol, sodium, protein, from each food??

--------------------
Foods placed in meal: chicken, steak

combined properties

calories: 88
carbohydrates: 100
fat: 62
cholesterol: 117
sodium: 68
protein: 145


The function needs to add up all the user entered foods from the meal function, not from the add function, and add up these things...

not sure how to do this, and for this assignment, i cant use pointers!! Damn it!!!
Not really sure what you're asking here. Do you just need to iterate through the list and sum up the foods' calories?
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 04-25-08   #6 (permalink)
Overclocker
 
B-80's Avatar
 
intel nvidia

Join Date: Oct 2006
Posts: 1,578

Rep: 71 B-80 is acknowledged by some
Unique Rep: 62
Trader Rating: 0
Default

// list[0] is chicken
//list[1] is steak
//list[2] is added

String names[1] = {firstFood, secondFood};

int list[2][6];
for(int i = 0; i<list[0].length(); i++)
{
list[2][i] = list[i][0]+list[i][1];
}

//display
__________________

Aumotocnic"An unfortunate member of the overclock.net insomnia club"

Crucial Ballistix Club


System: The Mediocre Guy
CPU
E7200 @ 3.3ghz
Motherboard
Gigabyte P45 DS3
Memory
4GB : 2 GB Crucial Ballstix DDR2 800 + 2GB Crap
Graphics Card
EVGA Nvidia Geforce 8800 GTS 320 MB
Hard Drive
250 gb hitachi Deskstar + 120gb Seagate
Power Supply
PC Power & Cooling Silencer 750 Quad
Case
Aspire X-Cruiser Black
CPU cooling
Zalman 9700
GPU cooling
Stock
OS
Windows xp

Last edited by B-80 : 04-25-08 at 03:31 AM.
B-80 is offline   Reply With Quote
Old 04-25-08   #7 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default

Ok, I think I just confused everyone. I'm typing this on my phone, so I can't upload the cpp file.

There is an enum with the food types in it. (meat = 1, fish = 2, pultury = 3, etc...)

I have a struct with the foods characteristics in it. (calories, fat, cholesterol, etc...)

I also have a list[50] in witch each element has each defined struct in it...

So for example:

List[0] has the data from the whole struct in it for chicken. List[1] has the whole struct for steak...

What I news to figure out is how to get inside each struct through a list[] argument...

Add all elements (calories, fat, cholesterol, etx...) of list[0] and list[1] together... But in a way that the user tells the app what list elements they want to add up. Anywhere from 2 up to how ever many enterys there are for foods imputed...

I'm grabbing some food now, I'll be back in a few to update this with my code and prototype.

I have no idea...
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)

Last edited by JoBlo69 : 04-25-08 at 07:57 AM.
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #8 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default

Alright, so I'm back on my PC and I'm posting the code...

Code:
#include <iostream>
#include <cctype>

using namespace std;

const int SIZE = 100;
const int CAPACITY = 100;

enum Category 
{
    unknown = -1, meat, poultry, seafood, dairy, vegetable,
    fruit, grain, sweet, nCategory 
};

struct foodType
{
    char        name[SIZE];        // name of the food
    Category    category;        // what kind of food
    float        calories;        // calories
    float        carbohydrates;    // grams
    float        fat;            // grams
    float        cholesterol;    // grams
    float        sodium;            // grams
    float        protein;        // grams
};


//Prototypes
void showMenu();                                                //Displays initial database options
void executeCommand (char command, foodType list[],int &size);  //case statment that executes command choices for the database options
void addFood(foodType list[], int& size);                       //adds food choices to the array
void listFood(const foodType list[], int size);                 //lists food on the screen

void calculateMeal();                                           // Need to figure out how to calculate each food entry's atributes

char getCommand();                                              //command choice from database options

void deleteFoodIndex(int i);
int readInteger(char prompt[]);


//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------

int main ()
{
    foodType foodList[CAPACITY];
    char menuOption = '1';
    int listSize = 0;

    showMenu();
    while(menuOption != 'q')
    {
        menuOption = getCommand();
        executeCommand(menuOption, foodList, listSize);
    }

    cout << "Thank you for using nutricalc 1.0 " << endl;


}

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------


//functions


void showMenu()
{
    cout << "Nutricalc 1.0" << endl;
    cout << "Implemented by: JoBlo" << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "edit food database" << endl;
    cout << "    Commands: a(dd), l(ist), d(elete) index, m(eal), q(uit)" << endl;
    cout << "-----------------------------------------------------------" << endl;
    cout << "" << endl;
    cout << "> ";
}

char getCommand()
{
    char option;
    cin.get(option);
    cin.ignore(100, 'n');
    return option;
}

void executeCommand (char command, foodType list[], int &size)
{
    switch (command)
    {
    case 'a':
    case 'A':
        addFood(list, size);
        break;
    case 'l':
    case 'L':
        listFood(list, size);
        break;
    case 'd':
    case 'D':
        //deteteFoodIndex();
    case 'm':
    case 'M':
        //calculateMeal(list, size);
    default:
        cout<< "Illegeal command" <<endl;
    
    }
}

void addFood(foodType list[], int& size)
{
    foodType aFood;
    char ch;

    cout << "name:               > ";
    cin.get(aFood.name, SIZE, 'n');
    cin.ignore(100, 'n');

    aFood.category = static_cast <Category> (readInteger("meat = 0, poultry =1, seafood = 2, dairy = 3, vegetable = 4, fruit = 5, grain = 6, sweet = 7"));     //I can get this part to work, but not able to get it to diplay correctly. Ideas??
    
    aFood.calories          = readInteger("calories:             > ");
    aFood.carbohydrates     = readInteger("carbohydrates:        > ");
    aFood.fat               = readInteger("fat:                  > ");
    aFood.cholesterol       = readInteger("cholesterol:          > ");
    aFood.sodium            = readInteger("sodium:               > ");
    aFood.protein           = readInteger("protein:              > ");
    cout << ""    << endl;
    cout << "> ";

    list[size++] = aFood;

}

void listFood(const foodType list[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << ""                                                                   << endl;
        cout << "index:          " << i                                              << endl;
        cout << "name:           " << list[i].name                                   << endl;
        cout << "category:       " << list[i].category                               << endl;
        cout << "calories:              " << list[i].calories         << ".00 grams" << endl;
        cout << "carbohydrates:         " << list[i].carbohydrates    << ".00 grams" << endl;
        cout << "fat:                   " << list[i].fat              << ".00 grams" << endl;
        cout << "cholesterol:           " << list[i].cholesterol      << ".00 grams" << endl;
        cout << "sodium:                " << list[i].sodium           << ".00 grams" << endl;
        cout << "protein:               " << list[i].protein          << ".00 grams" << endl;
        cout << ""                                                    << ".00 grams" << endl;
        cout << "> "                                                                        ;
    }
}

int readInteger(char prompt[])
{
    int temp;

    cout << prompt;
    cin >> temp;
    while(!cin)
    {
        //clear the error code of cin
        cin.clear();

        //remove the garbage on the input stream
        cin.ignore(100, 'n');

        //ask user to try again
        cout << "Illegal integer! Try again: ";

        //read in
        cin >> temp;
    }

    //remove the 'n'
    cin.ignore(100, 'n');

    return temp;
}

void calculateMeal()
{
    
}

void deleteFoodIndex()
{
    int i;
    
    cout << "Please enter food index to delete:" << endl;
    cin >> i;
    
    //for (i >= 0 ; i =< 50 ; x++);
    {
        
    }
        
    
}
And here are the binaries of what this program should do... (attached)
Attached Files
File Type: zip labs.Nutricalc 1.0 Prototype.zip (83.5 KB, 2 views)
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #9 (permalink)
Programmer
 
JoBlo69's Avatar
 
intel nvidia

Join Date: Jan 2007
Posts: 3,291

Rep: 168 JoBlo69 is acknowledged by manyJoBlo69 is acknowledged by many
Unique Rep: 145
Trader Rating: 15
Default

i need help with these two functions:

calculateMeal()
deleteFoodIndex()

Thanks for the help... Let me know if anyone needs clarification on what these two functions do.
__________________
My Lego case thread. With PICS!!!
-----------------------------------------------------------------------
Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.

System: 45nm FTW
CPU
E8400 3.6Ghz 1.25v
Motherboard
ASUS P5Q Delux P45
Memory
4GB (2x2GB) PATRIOT Viper PVS24G 6400LLK R
Graphics Card
XFX 8800GT
Hard Drive
2 x 74GB Raptors RAID0 ICH10R
Sound Card
onboard FTRW!!!
Power Supply
700w OCZ GamerXtream
Case
POS
OS
64bit Vista
Monitor
18" Dell LCD + 24" Gateway LCD (killed it's self)
JoBlo69 is offline JoBlo69's Gallery   Reply With Quote
Old 04-25-08   #10 (permalink)
Overclocker
 
Manyak's Avatar
 
intel nvidia

Join Date: Mar 2008
Posts: 2,155

Rep: 186 Manyak is acknowledged by manyManyak is acknowledged by many
Unique Rep: 134
Trader Rating: 12
Default

In all honesty, I think your code is very very messy. Actually, I'm not sure if it will even work at all.

You have:
Code:
int main ()
{
    foodType foodList[CAPACITY];
    ...
}
So your array that you are working with is only visible in your main function. If you want a seperate delete function for it, you have to actually pass it to that function as well. So instead of the void deleteFoodIndex() you have, do this:
Code:
void deleteFoodIndex(foodtype &list)
{
    cin >> i;

    for ( ; i<50; i++)
    {
        if (i<49)
        {
            *list[i] = *list[i+1];
        }
        else
        {
            *list[i] = NULL;  //Or whatever you want to use as a blank
        }
    }
And in fact, since you don't use a pointers to pass that around anywhere, you won't be able to retain the list after each loop. When you pass the array to a function without using a pointer, it makes a copy of the array in memory and the function works with that copy, not the original. So once you are done with that function the copy gets destroyed and you are back to nothing again.
__________________
System: Gravehouse
CPU
Intel E8400
Motherboard
Intel DX38BT
Memory
OCZ Platinum DDR3-1333
Graphics Card
Geforce 8800gts G80
Hard Drive
2x WD RE2 500GB RAID0
Sound Card
X-Fi Platinum
Power Supply
ABZ Tagan 800W
Case
CM Stacker 832
Manyak is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 07:04 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.23926 seconds with 10 queries