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];



}
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;

Edited 4 Years Ago by Lucaci Andrew

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?

Edited 4 Years Ago by rogernick

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?

Edited 4 Years Ago by WaltP

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

Edited 4 Years Ago by WaltP

//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;
 //
Comments
Don't tag the entire post as CODE. Tag ONLY the code!

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[]

This article has been dead for over six months. Start a new discussion instead.