Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can I make this C++ more efficient?
New Posts  All Forums:Forum Nav:

Can I make this C++ more efficient?

post #1 of 7
Thread Starter 
My purpose is to allow the user to input the array size "a[]" and create a second array b[] with the same size. The user will enter any integer to store in array a[] and b[]. Array c[] will sum the total for each array such as c[0]= a[0]+b[0], c[1]=a[1]+b[1]...etc and display it.

I want to make it more efficient by using pointer and pass by reference. Can someone help me on pointer and reference? I'm having a hard time on pointer and pass by reference. thx

Code:
#include<iostream>

using namespace std;

//prototype, cout function.
void inputArray(int&);

//prototype, calculate 2 arrays and store result on the c[]
void createArray(int [], int[], int[], int);

int main(){
    
    int arraySize;
    int aArray[0];
    int *aPtr = aArray;
    int bArray[0];
    int *bPtr = bArray;
    int cArray[0];
    int *cPtr = cArray;
    
    inputArray(arraySize);
    createArray(aArray, bArray, cArray, arraySize);

    
    //********************************************
    cin.ignore(256, '\
');
    cout << "\
\
Press ENTER to continue..." << endl;
    cin.get();

    return 0;
}
void createArray(int a[], int b[], int c[], int sizeOfArray){
    using namespace std;
    int inputFirstArray;
    int inputSecondArray;
    for(int i=0; i<sizeOfArray; i++){
        cout << "Please input 1st array at array[" << i << "] ";
        cin >> inputFirstArray;
        cout << "Please input 2nd array at array[" << i << "] ";
        cin >> inputSecondArray;
        a[i]=inputFirstArray;
        b[i]=inputSecondArray;
        c[i]=a[i]+b[i];
        cout << "Total for a[" << i
             << "] and b[" << i
             << "] is stored in c[" << i
             << "] : " << c[i] << "." << endl;
    }
}
void inputArray(int& arraySize){
    using namespace std;
    cout << "Input array size : ";
    cin >> arraySize;
}
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #2 of 7
if i remember my c++ correctly, the array name is a pointer to the first element in the array. i might be wrong but if you want to pass it a pointer you should call this:

Code:
createArray(aPtr, bPtr, cPtr, arraySize);
also, if you don't need to save the users input, you would save memory by not using arrays a and b at all.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 7
Thread Starter 
you are right, i think the array name is a pointer to the first element in the array. Should I use const on constant data?
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #4 of 7
Thread Starter 
I made this easier to read.
Code:
void createArray(int a[], int b[], int c[], int sizeOfArray){
    using namespace std;
    int inputFirstArray;
    int inputSecondArray;
    for(int i=0; i<sizeOfArray; i++){
        cout << "Please input 1st array at array[" << i << "] ";
        cin >> inputFirstArray;
        cout << "Please input 2nd array at array[" << i << "] ";
        cin >> inputSecondArray;
        a[i]=inputFirstArray;
        b[i]=inputSecondArray;
        c[i]=a[i]+b[i];
        cout << "Total for a[" << i
             << "] and b[" << i
             << "] is stored in c[" << i
             << "] : " << c[i] << "." << endl;
    }
}
to
Code:
void createArray(int a[], int b[], int c[], int sizeOfArray){
    using namespace std;
    for(int i=0; i<sizeOfArray; i++){
        cout << "Please input 1st array at array[" << i << "] ";
        cin >> a[i];
        cout << "Please input 2nd array at array[" << i << "] ";
        cin >> b[i];
        c[i]=a[i]+b[i];
        cout << "Total for a[" << i
             << "] and b[" << i
             << "] is stored in c[" << i
             << "] : " << c[i] << "." << endl;
    }
}
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by mkn1620 View Post
I made this easier to read.
Code:
void createArray(int a[], int b[], int c[], int sizeOfArray){
    using namespace std;
    int inputFirstArray;
    int inputSecondArray;
    for(int i=0; i<sizeOfArray; i++){
        cout << "Please input 1st array at array[" << i << "] ";
        cin >> inputFirstArray;
        cout << "Please input 2nd array at array[" << i << "] ";
        cin >> inputSecondArray;
        a[i]=inputFirstArray;
        b[i]=inputSecondArray;
        c[i]=a[i]+b[i];
        cout << "Total for a[" << i
             << "] and b[" << i
             << "] is stored in c[" << i
             << "] : " << c[i] << "." << endl;
    }
}
to
Code:
void createArray(int a[], int b[], int c[], int sizeOfArray){
    using namespace std;
    for(int i=0; i<sizeOfArray; i++){
        cout << "Please input 1st array at array[" << i << "] ";
        cin >> a[i];
        cout << "Please input 2nd array at array[" << i << "] ";
        cin >> b[i];
        c[i]=a[i]+b[i];
        cout << "Total for a[" << i
             << "] and b[" << i
             << "] is stored in c[" << i
             << "] : " << c[i] << "." << endl;
    }
}
if you want to use pointer you could do this

void createArray(int *a, int *b,int *c, int sizeOfArray)

change a[i] to *(a+i) and so on.

to initialize the pointers do this.

int *a, *b, *c;

a = new int[size];
and so on.

You should read up on the uses of pointer notation.
just remember when you want to store data into a pointer remember to use *x and not x,
example, memory location is 1234
int *a;
a = new int;

a = 1234; // this would assing the pointer to the memory address 1234
*a = 5; // this would store the number 5 at address 1234/

cout << a; would print out 1234
cout << *a; // would print out 5

good luck
Valery
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k GA-P67A-UD4-B3 Radeon 7970 Reference G. Skill Sniper 
Hard DriveOptical DriveOSMonitor
Corsair NOVA SSD 64gb + 500gb Storage + 1TB Storag HP DVD burner Windows 7 64bit 37" 1080p60hz 
KeyboardPowerCaseMouse
Razer Blackwidow Ultimate Rosewill Lightning 1000W Single Rail LianLi PC-K58 Razer Spectre 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
Valery
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k GA-P67A-UD4-B3 Radeon 7970 Reference G. Skill Sniper 
Hard DriveOptical DriveOSMonitor
Corsair NOVA SSD 64gb + 500gb Storage + 1TB Storag HP DVD burner Windows 7 64bit 37" 1080p60hz 
KeyboardPowerCaseMouse
Razer Blackwidow Ultimate Rosewill Lightning 1000W Single Rail LianLi PC-K58 Razer Spectre 
Mouse Pad
Razer Goliathus Speed 
  hide details  
Reply
post #6 of 7
Quote:
Originally Posted by mkn1620 View Post
you are right, i think the array name is a pointer to the first element in the array. Should I use const on constant data?
i dont see any data in your program that should obviously be const. but yes you would use const if you needed something to stay constant. const in c++ can be confusing especially when dealing with pointers. wikipedia has a decent article explaining how to use const. http://en.wikipedia.org/wiki/Const-correctness
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #7 of 7
Thread Starter 
thank you guys, its very helpful!
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can I make this C++ more efficient?