wrong output

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1520 Views - Last Post: 04 December 2012 - 04:16 PM Rate Topic: -----

#1 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

wrong output

Posted 03 December 2012 - 11:15 AM

#include <iostream>

using namespace std;
void swapArray(int  array1[], int  array2[],int size);

void swapArray(int array1[],int array2[], int size)
{
        int temp;
    for (int i=0;i<size;i++)
   {
    temp= array1[i];
    array1[i] = array2[i];
    array2[i] = temp;
    cout<<"array 1 elements is:"<<array1[temp]<<endl;
    cout <<"array 2 elements is:"<<array2[temp]<<endl;
}}
int main()
{
    int size=5;
    int array1[size];
    int array2[size];
    int element1,element2;

    array1[size] = element1;
    array2[size] = element2;
for (int i=0;i<size;i++)
 {cout<<"please Enter Element of The first array"<<endl;
 cin>>element1;
 cout<<"please Enter Element of The second array"<<endl;
 cin>>element2;
 swapArray (array1,array2,size);
 }


    return 0;
}



i wrote this code to swap elements in array to swap each element one by one but m not having to right output any highlights?

Is This A Good Question/Topic? 0
  • +

Replies To: wrong output

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: wrong output

Posted 03 December 2012 - 11:25 AM

You're using temp as an array index on lines 14 and 15. temp holds the original value of array1[i] and there's no guarantee that that's a valid index for either array1 or array2.

Also you're using a non-const int variable as the size of your statically allocated arrays. That's not legal in C++.

PS: It's generally considered good manners to explain what your input was, which output you expected and which output you actually got when something produces "wrong output".

This post has been edited by sepp2k: 03 December 2012 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 shadowevil  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 12-August 10

Re: wrong output

Posted 03 December 2012 - 11:53 AM

Your code completely did not work from the start... I commented what I did... I hope you can learn from this.

Good luck!

#include <iostream>
#include <conio.h>					// _getch() function
#include <Windows.h>				// ZeroMemory() function

using namespace std;

const int size=5;			// Needs to be constant value as array size will never change.	(Global Variable)
int array1[size];			// Assign as a Global Variable
int array2[size];			// Assign as a Global Variable
void swapArray(int element1, int element2, int i);		// Pass the information as element1 and element2 and current array position of i

int main()
{
	ZeroMemory(array1, sizeof(array1));			// ZeroMemory() a function to allocate memory of an array value.
	ZeroMemory(array2, sizeof(array2));			// ZeroMemory() a function to allocate memory of an array value.

	int element1(0),element2(0);				// set values to 0 (same as element = 0;)

	for (int i=0;i<size;i++)
	{
		cout<<"please Enter Element of The first array"<<endl;
		cin>>element1;
		cout<<"please Enter Element of The second array"<<endl;
		cin>>element2;
		swapArray(element1,element2,i);			// Pass user input variables, and array position.
	}

	for(int i=0;i<size;i++)				// Display the arrays and positions.
	{
		cout<<"\n\nArray 1 elements is:"<<array1[i]<<endl;			// Escape key \n is a endl
		cout <<"\nArray 2 elements is:"<<array2[i]<<endl;
	}
	_getch();			// Wait for key press (pause application)
	return 0;
}

void swapArray(int element1,int element2, int i)
{
	int temp(0);
	array1[i] = element1;			// Set array1[i] to element1
	array2[i] = element2;			// Set array2[i] to element2
	temp = array1[i];				// Set temp to array1[i]
	array1[i] = array2[i];			// Set array1[i] to array2[i]
	array2[i] = temp;				// Set array2[i] to temp
}


This post has been edited by shadowevil: 03 December 2012 - 11:54 AM

Was This Post Helpful? -1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: wrong output

Posted 03 December 2012 - 12:05 PM

There's no reason to make the arrays global. That's just horrendously bad design and completely destroys the reusability of the swapArray function.
Was This Post Helpful? 0
  • +
  • -

#5 shadowevil  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 12-August 10

Re: wrong output

Posted 03 December 2012 - 12:07 PM

View Postsepp2k, on 03 December 2012 - 12:05 PM, said:

There's no reason to make the arrays global. That's just horrendously bad design and completely destroys the reusability of the swapArray function.

Well considering it works for what he is probably wanting... And I was having trouble passing the arrays through the function, so I passed it as a global variable.
Was This Post Helpful? 0
  • +
  • -

#6 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

Re: wrong output

Posted 03 December 2012 - 12:10 PM

  ZeroMemory(array1, sizeof(array1));
 ZeroMemory(array2, sizeof(array2));


what is these statements mean? what is the sizeof and can we replace it with another this?
Was This Post Helpful? 0
  • +
  • -

#7 shadowevil  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 12-August 10

Re: wrong output

Posted 03 December 2012 - 12:12 PM

View Postd00n, on 03 December 2012 - 12:10 PM, said:

  ZeroMemory(array1, sizeof(array1));
 ZeroMemory(array2, sizeof(array2));


what is these statements mean? what is the sizeof and can we replace it with another this?


ZeroMemory() is a function to allocate memory space for the array.

sizeof() is basically "size" or 5... because the size of the array is 5.

The ZeroMemory() function could also look like this:

ZeroMemory(array1, size);          // size is equal to 5 and the array size is also 5



variableType arrayName[SizeOfArray];


This post has been edited by shadowevil: 03 December 2012 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: wrong output

Posted 03 December 2012 - 12:33 PM

@shadowevil, ZeroMemory() is a Function-Like Macro containing memset()... It is declared as:
#define ZeroMemory(p, sz) memset((p), 0, (sz))


It doesn't allocates any memory... The memset() function contained in it just sets a block of memory with a value up to a size number of bytes...

So, by using ZeroMemory(), the block of memory is filled with 0's on the first size number of bytes...

Edit: Spelling correction...

This post has been edited by AKMafia001: 03 December 2012 - 12:34 PM

Was This Post Helpful? 3
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2086
  • View blog
  • Posts: 3,173
  • Joined: 21-June 11

Re: wrong output

Posted 03 December 2012 - 12:41 PM

View PostAKMafia001, on 03 December 2012 - 08:33 PM, said:

@shadowevil, ZeroMemory() is a Function-Like Macro


And of course, given that it's defined in windows.h, it's Windows specific. So you shouldn't use it if you want your programs to be portable. The same goes for conio.h.
Was This Post Helpful? 3
  • +
  • -

#10 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

Re: wrong output

Posted 03 December 2012 - 12:42 PM

what about if i dont want to use the global.. where should i post it int the main
Was This Post Helpful? 0
  • +
  • -

#11 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: wrong output

Posted 03 December 2012 - 01:04 PM

Well! A good coding style is to declare your variables in the most inner block so they would get destroyed once they go out of scope..

It is also good to declare your variables when they are needed, so you would know for what purpose they are used...
e.g.
std::cout << "Enter a number: ";
int nNum;
std::cin >> nNum;



Another example:
{
    int x = 0;
    std::cout << x;    // prints 0
    std::cout << "I'm  in outer block!";

    {
        int x = 1;
        std::cout << x;    // prints 1
        std::cout << "I'm in inner block!";
    } // x will go out of scope and will get destroyed

    std::cout << x;    // prints 0 again, this x is not the same as of the one in the inner block
}



Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

#12 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

Re: wrong output

Posted 03 December 2012 - 01:30 PM

thanks

#include <iostream>
using namespace std;

	void swapArray(int array1[], int array2[], int i);
	void swapArray(int array1[], int array2[], int i)
	{
	   int temp = array1[i];               // Set temp to array1[i]
	    array1[i] = array2[i];          // Set array1[i] to array2[i]
	    array2[i] = temp;               // Set array2[i] to temp

	}

	int main()
	{

        const int size=5;
        int array1[size];
        int array2[size];
	    int element1=0,element2=0;

	    for (int i=0;i<size;i++)  // to take the 5 elements from the user
	    {
	        cout<<"please Enter Element of The first array"<<endl;
	        cin>>element1;
            cout<<"please Enter Element of The second array"<<endl;
            cin>>element2;
                 array1[i] = element1;      //then save them in the array to send them to the function and swap them
                 array2[i] = element2;
        swapArray(array1,array2,i);  //function call
                	    }

	    for(int i=0;i<size;i++)              // Display the arrays
	    {
	        cout<<"\n\n Array 1 elements is:"<<array1[i]<<endl;
	        cout <<"\n Array 2 elements is:"<<array2[i]<<endl;
	    }

	    return 0;
	}




that us works good but what if i want to put the loop in the function itself what would be change?
and what about if i want to try solve it recursively
Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5163
  • View blog
  • Posts: 26,834
  • Joined: 10-May 07

Re: wrong output

Posted 03 December 2012 - 01:33 PM

View Postd00n, on 03 December 2012 - 03:30 PM, said:

but what if i want to put the loop in the function itself what would be change?

How about trying it yourself?
Was This Post Helpful? 0
  • +
  • -

#14 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

Re: wrong output

Posted 03 December 2012 - 01:42 PM

hmm i tried to out the second array that display the element but i got crashed everything the output looks wired
Was This Post Helpful? 0
  • +
  • -

#15 shadowevil  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 12-August 10

Re: wrong output

Posted 03 December 2012 - 03:58 PM

View PostAKMafia001, on 03 December 2012 - 12:33 PM, said:

@shadowevil, ZeroMemory() is a Function-Like Macro containing memset()... It is declared as:
#define ZeroMemory(p, sz) memset((p), 0, (sz))


It doesn't allocates any memory... The memset() function contained in it just sets a block of memory with a value up to a size number of bytes...

So, by using ZeroMemory(), the block of memory is filled with 0's on the first size number of bytes...

Edit: Spelling correction...

Well... If I don't use ZeroMemory() It crashes my program saying, "Could not read from location." etc etc...

View Postsepp2k, on 03 December 2012 - 12:41 PM, said:

View PostAKMafia001, on 03 December 2012 - 08:33 PM, said:

@shadowevil, ZeroMemory() is a Function-Like Macro


And of course, given that it's defined in windows.h, it's Windows specific. So you shouldn't use it if you want your programs to be portable. The same goes for conio.h.

And I agree... IF you want to use this on other OS's more measures are to be taken to achieve that... But I don't program for Linux, or MAC OS or such other Operating Systems.

The header file "conio.h" is only for a pause function... It would be commented out if it was to be distributed...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2