954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays Assignment!!!

I have this assignment that requires a program that will initialize an array of charcters to ABCDE and allow you to shift in a circular fashion, those characters to the right any number of places you specify.The shifting shoud be done in the functino shift() but all printouts should be from main the program does not repeat. I have started but I have no idea where to go from there.Any ideas?

#include
using namespace std;

//function prototype
void shift(char,int);

int main()
{
//declare variables
int places=0;
int count=0;

//declare array
char chrs[5]={'A','B','C','D','E'};
char temp[5]={' ',' ',' ',' ',' '};

//Display
shift(chrs,places)

cout<<"Before shifting:"<>places;

if(places!=0)
{

cout<<"After shifting"<

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I kind of just did your assignment for you :cheesy: I was interested in trying it out. Basically this uses the ASCII values for the characters to shift them and that helps make it nice and neat.

#include "stdafx.h"
     #include <iostream>
     using namespace std;
     
     void ShiftLetters(char *buffer, int places);
     
     int main(int argc, char* argv[])
     {
     	int PlacesToMove = 0;
     	int temp = 0;
     	char OurLetters[5] = {'A','B','C','D','E'};
     
     	cout << "Letters before shift: ";
     	
     	//Need to do the following because OurLetters is not null-terminated
     	for (temp = 0; temp < 5; temp++)
     	{
     		cout << OurLetters[temp];
     	}
     
     	cout << "\n\n" << "How many places right should the letters be shifted? ";
     	cin >> PlacesToMove;
     
     	if (PlacesToMove != 0)
     	{
     		ShiftLetters(OurLetters, PlacesToMove);
     		cout << "Letters after shift: ";
     		for (temp = 0; temp < 5; temp++)
     		{
     			cout << OurLetters[temp];
     		}
     
     		cout << "\n";
     	}
     
     	return 0;
     }
     
     void ShiftLetters(char *buffer, int places)
     {
     	places = places % 5;
     
     	for (int i = 0; i < 5; i++)
     	{
     		buffer[i] = buffer[i] - places;
     		if (buffer[i] < 65)
     		{
     			buffer[i] = buffer[i] + 5;
     		}
     	}
     }


The biggest thing you're probably interested in is this:

void ShiftLetters(char *buffer, int places)
      {
      	places = places % 5;
      
      	for (int i = 0; i < 5; i++)
      	{
      		buffer[i] = buffer[i] - places;
      		if (buffer[i] < 65)
      		{
      			buffer[i] = buffer[i] + 5;
      		}
      	}
      }


So I'll go over that. At the beginning it just places modulus 5 because there are really only 5 different shifts you can execute: right 1, 2, 3, 4, or 5 (or 6 if you count 0), so its easier to modulus the places by 5 to make the shift one of those values.

Then it goes through each letter and subtracts the places from it to receive the new ASCII value. If its less than 65 (less than 'A'), it adds 5 to put it back into the correct value.

Thats basically it.

==========================================================

To critique your code, and tell you some things you can improve on:

cout<<"Before shifting:"<<chrs[5]<<endl;


That won't work because:chrs has only 5 elements. Remember, in C++ (and most other programming languages) the indexes start at 0!
chrs is not null-terminated so you cant just do "chrs" either. You have to loop it like I did above.
void shift(char chrs[5],int places)


An array of chars is already the same as a char * in a sense. So instead of calling for chrs[5], call for a char*. THen you can make changes directly to the character array.

MitchellH
Newbie Poster
12 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

mmm... ps quien sabe

vyk2rr
Newbie Poster
7 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

Okay #1,Thanks for the help!
#2 I only a begginer so I really don't understand about 60% of it
#3 one of the sample outputs says shift 8 places so i'm pretty sure it has to shift more than five places without looping and tempt is an array used int the shift function.

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
#3 one of the sample outputs says shift 8 places so i'm pretty sure it has to shift more than five places without looping and tempt is an array used int the shift function.


It can shift however many spaces you want it to (as long as its less than 32000 however many that an int can hold :))

MitchellH
Newbie Poster
12 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 
Okay #1,Thanks for the help! #2 I only a begginer so I really don't understand about 60% of it #3 one of the sample outputs says shift 8 places so i'm pretty sure it has to shift more than five places without looping and tempt is an array used int the shift function.

and by having other do your homework for you you'll never understand anything more about it...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
and by having other do your homework for you you'll never understand anything more about it...

You're right, any help on improving the one I did already simply?

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
You're right, any help on improving the one I did already simply?


Don't use magic numbers like 5 and 65.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks I think I 'll try writing it again

boujibabe
Junior Poster
123 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You