Sign in to follow this  

2D arrays in c++

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im making a console c++ program for a personal assignment, and a part of it needs operations on an array.
    A B C D E F G H I...Z

A   a b c d e f g h i...z
B   b c d e f g h i...z a
C   c d e f g h i...z a b
D   d e f g h i...z a b c
...
Z z
(capital letters denote a referencing row)
Operation - 1. User picks two letters from the reference rows (eg 'C' and 'D') 2. Program shows the alphabet which occours in the same row and column as both of the reference letters (here that'd be 'f', for 'B' and 'G' that'd be 'h', etc) Could anyone please give me the code fragment for the two given operations? -regards, BlueBolt [Edited by - BlueBolt on September 17, 2007 8:16:39 AM]

Share this post


Link to post
Share on other sites
@SiCrane - Of course...just asked one.
Quote:
Original post by ToohrVyk
You don't need operations on arrays for this. The structure of values inside the arrays is so regular that you can generate it on the fly using two '+' and one '%'.

I tried to use the ASCII of a character to generate the entire English alphabet (as a starting step to implement your advice)...it just spewed me with junk values...

#include<iostream>
using namespace std;
int main()
{
char alpha;
alpha=1; //or maybe alpha=a;
cout<<alpha;
cin>>alpha;
for(int i=0; i<=26; alpha++)
cout<<alpha;
return 0;
}

I have a gist that if I knew how to represent alphabets in their ASCII code in the program, I would be able to make the alphabetic sequences (a matter of an increment sign, as ToohrVyk said...dunno where the modules pops in)

Stuck again...please help.
-regards,
BlueBolt

Share this post


Link to post
Share on other sites
Quote:
Could anyone please give me the code fragment for the two given operations?


The answer: "Yes, people here can give you the advice".

But, given that the entire structure of the OP is very much like a homework assignment, most would be unwilling to do so.


Quote:
for(int i=0; i<=26; alpha++)

cout<<alpha;


Letters are represented as ASCII table. But since you can treat them as numbers in your particular case, then this becomes valid:

for (char c = 'a'; c <= 'z'; c++) std::cout << c;


The other way, to obtain n-th character, can be obatined using:

int index = 14;
std::cout << ( 'a' + index );


Obviously, it goes the other way as well. Which number is the letter x?
char x = 'f';
std::cout << (x - 'a');


And no, there is no need for a table, everything can be calculated using the above operations.

Share this post


Link to post
Share on other sites

This topic is 3738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this