Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 648083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:47:37+00:00 2026-05-13T21:47:37+00:00

VISUAL C++ Question Hi, I have array of 3 elements and I want to

  • 0

VISUAL C++ Question

Hi,

I have array of 3 elements and I want to shift its elements to the right and replace the shifted index cell with “SHIFTED” string and this should loop until all the cells has “SHIFTED” string.


For example:

int a[x]={0,1,2};

Initial index and elements Order:

[0]=0
[1]=1
[2]=2

should become in the:

1st loop:

 [0]=SHIFTED
 [1]=0
 [2]=1

2nd Loop:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=0

3rd Loop:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=SHIFTED

I know I can do that with memmove() but I don’t want to use any function in it.

Would you please help me; here is my work:

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;

int const ARRAY_SIZE=3;

int main()
{
 int Array[ARRAY_SIZE];
 int iniPostion,newPostion;
 string x="SHIFTED";
 for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++)
 {
  Array[iniPostion] = iniPostion;
  cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
 }
 cout << endl;
 for(newPostion=0; newPostion<ARRAY_SIZE; newPostion++)
 {
  Array[newPostion]=newPostion;
  cout << "Cell [" << newPostion << "]     New Element is: (";
  if(Array[newPostion-1]<0)
  {
   cout << x << ")\n";
  }
  else
  {
   cout << Array[newPostion-1] << ")" << endl;
  }
 }
 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:47:37+00:00Added an answer on May 13, 2026 at 9:47 pm

    As Peter mentioned in his answer, you cannot assign a string to a int. I’ve assumed SHIFTED to be -1. So every time you shift you bring in a -1 in the gap created.

    You need two loops. The outer loops iterates N (3) times and inner loop starts at the end of the array and copies (n-1)th element to nth position:

    for(int count = 0;count < N;count++){
        for(newPostion=ARRAY_SIZE-1; newPostion > count;newPostion--)
            Array[newPostion]=Array[newPostion - 1]; // copy
        Array[newPostion]= -1; // fill the gap.
    
        // print.
        for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++) {
            cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
        }
        cout<<endl;
    }
    

    Sample run:

    # g++ a.cpp && ./a.out
    Cell [0] Initial Element is: (0)
    Cell [1] Initial Element is: (1)
    Cell [2] Initial Element is: (2)
    
    Cell [0] Initial Element is: (-1)
    Cell [1] Initial Element is: (0)
    Cell [2] Initial Element is: (1)
    
    Cell [0] Initial Element is: (-1)
    Cell [1] Initial Element is: (-1)
    Cell [2] Initial Element is: (0)
    
    Cell [0] Initial Element is: (-1)
    Cell [1] Initial Element is: (-1)
    Cell [2] Initial Element is: (-1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a dumb question, but I have the following behavior in Visual
I know about this question: Which (third-party) debug visualizers for Visual Studio 2005/2008 do
This was an interview question. Given Visual Studio 2008 and an icon saved as
This is a subjective question. I worked in Visual Basic 6.0 before coming into
This is Crystal Reports 9 in Visual Studio 2003 by the way Simple question
I have tried to implement Stack Overflow question C++ Data Member Alignment and Array Packing
I have the following question: Does Microsoft Visual Studio (I'm using 2008 SP1) offer
I have a basic question. If I run an executable file (Release, Visual Studio
According to an earlier question about Visual Studio configurations , there's no way to
A question was asked recently about removing SourceSafe integration from Visual Studio 6 .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.