I'm having problems on replacing an inputed number without using gotoxy....im using TURBO c++ 4.5 compiler btw...

Here's a sample output should be like this

Enter elements: 1 2 3 4 5 6 7 8 9 10

Enter position:3
Enter replaced value: 14

New elements: 1 2 14 5 6 7 8 9 10

Here's what i've done so far

#include<iostream.h>
void main()

{
 const int numb=10;
 int arr[numb],x,y;
        cout<< "Enter elements:";
        cout<< "\n ";
 for(int i=0; i<10; i++ )
    {
        cout<< "\n ";
        cin>>arr[i];
        cout<< " ";
    }

 cout<< "\nEnter Position:";
 cin>>x;
 cout<< "\nEnter value to be inserted:";
 cin>>y;
 cout<< "\nNew elements:";
 cout<<arr[numb];



}

Recommended Answers

All 14 Replies

cout<< "\nEnter Position:";
 cin>>x;
 cout<< "\nEnter value to be inserted:";
 cin>>y;
 cout<< "\nNew elements:";
 cout<<arr[numb];

wouldn't that be

    cout<< "\nEnter Position:";
     cin>>x;
     cout<< "\nEnter value to be inserted:";
     cin>>y;
     if (x<numb){
        arr[x]=y;
    }
    else cout<<"Invalid position.";
     cout<< "\nNew elements: "<<arr[x]<<endl;

To display the contents of an array you need to use a loop.

I'm having problems on replacing an inputed number without using gotoxy....im using TURBO c++ 4.5 compiler btw...

Here's a sample output should be like this

Enter elements: 1 2 3 4 5 6 7 8 9 10

Enter position:3
Enter replaced value: 14

New elements: 1 2 14 5 6 7 8 9 10

Here's what i've done so far

#include<iostream.h>
void main()
{
 const int numb=10;
 int arr[numb],x,y;
        cout<< "Enter elements:";
        cout<< "\n ";
 for(int i=0; i<10; i++ )
    {
        cout<< "\n ";
        cin>>arr[i];
        cout<< " ";
    }
 cout<< "\nEnter Position:";
 cin>>x;
 cout<< "\nEnter value to be inserted:";
 cin>>y;
 cout<< "\nNew elements:";
 cout<<arr[numb];
}
------------
Did you try using a loop method?

Try using loop for inputing and displaying array, and for replacing, you can access array by its index. Also keep an condition statement for checking whether input is authentic or not.

Lucaci Andrew basically said: "test for invalid input"
NathanOliver said ... "you need to use a loop"

After these two posts:

rogernick said "Did you try using a loop method?"
np complete said "Try using loop for inputing and displaying ... checking whether input is [invalid] or not"

It helps to have an original thought, not just parrot what everyone else said. Was your wording better or more understandable?

I thought what I said was pretty straight forward.

Me too. But rogernick and np complete must have thought you were confusing.

still, having problems...phew

still, having problems...phew

Can't help you with just that, would ya mind posting more details

oh sorry about that

//Okay, another try...
//so I made an array that let's me input 10 numbers.



    #include<iostream.h>
            void main()
            {
             const int numb=10;
             int arr[numb],x,y;
                      cout<< "Enter elements:";
                      cout<< "\n ";
             for(int i=0; i<10; i++ )
                 {
                      cout<< "\n ";
                      cin>>arr[i];
                      cout<< " ";
                 }






 //with this i assigned elements from index 0 to 9...the problem is i need to replace the element on a certain index ....so i added this code:

//after that, I need to display the new array...for example the first array contained:


 //arr[]:{1 2 3 4 5 6 7 8 9 10} 

    cout<< "\nEnter Index:";
    cin>>x;
    cout<< "\nEnter value to be inserted:";
    cin>>y;



 //then i changed the element in index 0 to 9 so the output should be:

 //New elements: 9 2 3 4 5 6 7 8 9 10..

 //i think i need to use a loop, so this is what i've added to my code so far...but it's effin wrong..

         for(x<10;x++;)
         arr[x]=y;
 //
commented: Don't tag the entire post as CODE. Tag ONLY the code! -3

you can do it like it:

#include <iostream>
using namespace std;

int main(){
    int arr[10], pos, nr;
    for (int i=0;i<10;i++){
        arr[i]=i;
    }//puts in the array at index 0 for example nr. 0, at index 1, nr. 1 etc.
    cout<<"Insert position: ";
    cin>>pos;//asks for a position.
    if (pos<10){//validates the position
        cout<<"Insert element: ";//if position is good asks for an element.
        cin>>nr;
        arr[pos]=nr;
    }
    else cout<<"Invalid position.\n";
    for (int i=0;i<10;i++){//prints the array, using a loop.
        cout<<"Arr["<<i<<"] = "<<arr[i]<<endl;
    }
    return (0);
}

how do i do this question? input 10 numbers in single dimensional array A[] and store only those numbers that are even in array B[]

@ashhar.qureshi2 please start your own topic if you have a question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.