2D arrays in c++

Started by
4 comments, last by Antheus 16 years, 7 months ago
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]
Advertisement
Do you have an actual question?
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 '%'.
@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
char alpha = 'a';
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.

This topic is closed to new replies.

Advertisement