The variable cp_arr has been declared as an array of 26 pointers to char. Allocate 26 character values, initialized to the letters 'A' through 'Z' and assign their pointers to the elements of cp_arr (in that order).
int *arr1[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
arr1=cp_arr[0];
for(int i = 0; i < 26; i++)
cout << cp_arr[i];
is the newest,
char a ='A', b = 'B', c = 'C', d='D', e='E', f='F', g='G', h='H', i='I', j='J', k='K',l='L', m='M', n='N', o='O', p='P', q='Q', r='R', s='S', t='T', u='U', v='V', w='W', x = 'X', y='Y', z='Z';
cp_arr[0] = a;
cp_arr[1] = b;
cp_arr[2] = c;
cp_arr[3] = d;
cp_arr[4] = e;
cp_arr[5] = f;
cp_arr[6] = g;
cp_arr[7] = h;
cp_arr[8] = i;
cp_arr[9] = j;
cp_arr[10] = k;
cp_arr[11] = l;
cp_arr[12] = m;
cp_arr[13] = n;
cp_arr[14] = o;
cp_arr[15] = p;
cp_arr[16] = q;
cp_arr[17] = r;
cp_arr[18] = s;
cp_arr[19] = t;
cp_arr[20] = u;
cp_arr[21] = v;
cp_arr[22] = w;
cp_arr[23] = x;
cp_arr[24] = y;
cp_arr[25] = z;
for(int i = 0; i < 26; i++)
cout << cp_arr[i];
cout << endl;
both worked in visual but not myprogramming lab there has to be a better code for this but these were the only ones that worked
5 answers
Anyway, many of the repetitive initialization could have been simplified.
Here's an example code that works in Dev C++. See if this can inspire what you want to to.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char *arr1=alphabet; // pointer to beginning of char array
char *cp_arr[26]; // array of 26 char pointers
int i;
for(i = 0; i < 26; i++)cout << *(arr1+i); // print 26 characters using pointer arr1
cout << endl;
for(i=0;i<26;i++)cp_arr[i]=arr1+i; // assign 26 pointers to array
for(int i = 0; i < 26; i++)cout << *cp_arr[i]; // print 26 char. from pointer array
cout << endl;
system("PAUSE");
}
It is possible that these labs require a very precise way to present your answer/program, so it does not "work" when it can actually compile elsewhere.
One way to find out further is to provide (multiple) screen shots on the requirements (instructions) and the answer, together with (very important) the error message. Perhaps we can get somewhere with it. You can (most of the time) post links to Imageshack or other file servers for the screen shots.
for (int i=0;i<26;i++)
/*because the i counter will link to a specific array value, it will push into all the array fields (0-25). Then you can define what goes into the array each loop iteration by setting a dynamic character assignment for each value 'A'-'Z' using simple math: i will increase by 1 each time, and you can add that to the char value A' (which is set to an ascii code that increments in values of one)*/
{cp_arr[i]=new char('A'+i);}
When the program runs it loops like this:
i=0, cp_arr[0]= 'A'+0
i=1, cp_arr[1]= 'B' //'A'+1
.... and so on until
i=25, cp_arr[25]='Z' //'A'+25
I know this question is old but it might help future students understand some simpler coding.