Friday, May 21, 2010

C - plus plus array processing question?

how do you solve for an array index if you know the element of the array, but need to 'peal' everything off to find it's index number. Assuming that there is one and only one item in each element such as - cyper code for example ==


index 0 / 1/ 2/ 3/ 4/ 5/ 6/ 7 /8/9 /10/11/12/13/ ........


element c/ d/ e/ f/ g/ h / i / j / k / l/ m/ n/ o/ p/


I have a an arry of encrypted text such as hiddenMessage[1000]and I have the cypher ---- cypher_array[26] and have an empty arrary to fill in such as vola[ 1000] I've made them all myself. But now I need vola[ i ] = 'a' + ix; where ix is the index to the code. I tried just taking the letter in hiddenMessage[ix] and adding 'a' , but this does not yield the correct index number to the cypher code array! Soooo, if I have a letter from an encrypted array, how do I find the corresponding index of that letter in the cyper array ?

C - plus plus array processing question?
I think you'll have to search for the letter in the cypher array. If there's no pattern to the cypher, you'll need a loop that iterates through the cypher array until the letter is found. At that point, you'll have the index.
Reply:So cypher_array[] is an array of the 26 lowercase letters, where cypher_array[0] is the character associated with the lowercase letter 'a'?





If that's the case, then vola[i] = cypher_array[hiddenMessage[i] - 'a'];





Otherwise, maybe give more detail?
Reply:the truth is there is not enough info to really know what you want really for example:


-if the encrypted massage is number that repersent a place in the the key array the it will look some thing like this:


for (int i=0;i%26lt;size;i++){


vola[i]=cypher_array[hiddenmassage[i]];


}


-and if its the same thing but is has been shifed by 'a' it will look like this:


for (int i=0;i%26lt;size;i++){


vola[i]=cypher_array[hiddenmassage[i]+'a...


/*({[or]})*/


for (int i=0;i%26lt;size;i++){


vola[i]=cypher_array[hiddenmassage[i]]+'...


there is to much we can not know from your question sorry that i could not help you that much but this is what i got


How can I make an array named X to hold MAXSIZE strings in C++?

make the int 'const' and it will/should work.


const int MAX_SIZE = 9999;

How can I make an array named X to hold MAXSIZE strings in C++?
Use pointers to create a dynamic array. Use new to dynamically allocate the size equal to MAXSIZE of string


Quick question about Arrays?

When an array parameter is passed to a function





a. the elements of the actual array parameter are copied into the elements of the formal array parameter.





b. the elements of the formal array parameter are copied into the elements of the actual array parameter.





c. the formal parameter is a pointer that holds the address of the actual array parameter.





d. the programmer must write code which allocates enough space for the function to store the array.





I got this wrong on a Comp Sci exam and I'm just wondering what the answer is

Quick question about Arrays?
Actually, the answer is language dependent. But it's not d, as I'm not aware of any language that behaves in such a manner.





I'll assume you refer to the C model for arrays. Take a look at http://c-faq.com/aryptr/aryptrparam.html





So the answer would be C. Pointer-array equivalence is a tricky topic, and I recommend you spend some time on the C FAQ site, reading the answers.





EDIT:





You've got conflicting answers, with some claiming the answer is a and some c. Again, the answer is language dependent.





However, in C or C++, there is no question. When passing an array, it's almost like a pass by reference. Or you can think of it equivalently as passing a pointer. There is no copying involved. Yes, the everything else has a pass by value. That is not true for C.





If you don't believe me, you can test it out for yourself. Pass an array to a function, and modify the array in the function. The original values should have changed.





NOTE:


This is a tricky question. The equivalence of pointers and arrays is a bit confusing. It helps to have sites like C FAQ, usenet references, cprogramming.com, gamedev.net, and other places where there's reference material on C. Getting K%26amp;R's book also helps.
Reply:Ummm... Choice a. This is because that any parameter passed without a pointer or by reference (I'm assuming you're referring to C++ here) will be copied as an automatic storage class variable and can be called by the function within its scope by the formal variable name.
Reply:c
Reply:this would depend on which language you are coding in


in c, the answer is a


in java, the answer is c


Storing a name in array using C?

When the program starts I need to prompt for the Users name and store it in an array . I need to be able to frequently insert it in the printf("Ben (this was entered earlier) , please enter blah blah...") like so. Can someone please help me with this. I appreciate any help , or even a link where I can look it up as I was not able to find much on this . Thank you :)

Storing a name in array using C?
There are two ways, select which you feel suitable


first


char *name; // no matter if name istoo long


printf("Enter your name:");


scanf("%s",name); //or you can use


//gets(name);


//to print simply


printf("%s",name);





or by using arrays


char name[20]; //length limited upto 20 chars only


printf("Enter name:");


scanf("%s",name);


printf("%s",name);
Reply:http://www.scit.wlv.ac.uk/cbook/chap6.ar...





#
Reply:I don't get a full picture of what you want out of your question. I presume you are trying to deal with some kind of array of structured objects.


This site might guide you through..


http://vergil.chemistry.gatech.edu/resou...

sd cards

C++ array?

#include %26lt;string%26gt;


#include %26lt;iostream%26gt;





using std::cout;


using std::cin;


using std::endl;


using std ::string;





int main ()


{





string numberMonth[12]= {"Janurary","Feburary","March","April","...


string flowerOfmonth [12]={" "};


flowerOfmonth[0]="Carnation";


flowerOfmonth[1]="Violet";


flowerOfmonth[2]="Daffodile";


flowerOfmonth[3]="Daisy";


flowerOfmonth[4]="Lily-of-the-Valley";


flowerOfmonth[5]="Rose";


flowerOfmonth[6]="Water Lily";


flowerOfmonth[7]="Gladiolus";


flowerOfmonth[8]="Aster";


flowerOfmonth[9]="Cosmos";


flowerOfmonth[10]="Chrysanthemum";


flowerOfmonth[11]="Narcissus";





//get number of month


cout %26lt;%26lt; "Enter month (1-12) :" %26lt;%26lt; endl;


cin %26gt;%26gt;numberMonth;


if (month %26gt;= 1 %26amp;%26amp; month %26lt;= 12)


cout %26lt;%26lt; flowerOfmonth[numberMonth - 1] %26lt;%26lt; endl;


//else


cout %26lt;%26lt; "Invalid month" %26lt;%26lt; endl;


//end if


//end display salary





return 0;


} //end of main function








why cant i get this to work..thanks

C++ array?
You are expecting users to enter NUMERIC representation of the months. The wording of the months is NEVER used. So, numberMonths array definition is not needed.





Define numberMonths as an INTEGER.





Look what exactly you are doing with the numberMonth variable! It's an index for an array. Yet, you defined it as string.
Reply:try to chaneg this line


string flowerOfmonth [12]={" "};


to


string flowerOfmonth [12];


OR


initialize the second array as you have done with the first one


string flowerOfmonth = {"Carnation", "Violet", "Daffodile", ... }


How to make tic tac toe using c language program?

how to make a tic tac toe using the c language program using two dimensional array?

How to make tic tac toe using c language program?
#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;








void drawBoard(int board[][3]);


int getScore(int board[][3], int*, int*);


int gameover(int board[][3], int);





int main(int argc, char *argv[])


{


enum player { computer = 3, human = 2, tie = 1 };


int board[3][3] = { {0,0,0},{0,0,0},{0,0,0} };


int game = 0, turn = 0;





while (1)


{


int choice, row, column, howMany, newScore;


int biggestScore[9][3];


for(int i = 0; i %26lt; 9; i++) // Set biggestScore[9][3] to all zeros.


for (int j = 0; j %26lt; 3; j++)


biggestScore[i][j] = 0;





if(turn == 9) // No more sqares left


if(gameover(board, tie)) break;


else { game++; turn = 0; continue; }





else if((game + turn) % 2 == 0)


{


drawBoard(board);


cout %26lt;%26lt; "Select a square (1-9): ";


cin %26gt;%26gt; choice;


if(choice %26gt;= 1 %26amp;%26amp; choice %26lt;= 9)


{


// Change user input to row/column for use in 2D board array.


row = (int) (choice - 1) / 3;


column = (choice - 1) % 3;





// Check user's input


if(board[row][column]) { cout %26lt;%26lt; "Space taken, select again.\n"; continue; }


else { board[row][column] = human; turn++; }





if(getScore(board, %26amp;row, %26amp;column) %26gt; 50)


if(gameover(board, human)) break;


else { game++; turn = 0; continue; }


}


}


else


{


for(int x = 0; x %26lt; 3; x++)


for(int y = 0; y %26lt; 3; y++)


if(!board[x][y]) // Square is empty


{


newScore = getScore(board, %26amp;x, %26amp;y);


if(newScore %26gt; biggestScore[0][0])


howMany = 0;


if(newScore %26gt;= biggestScore[0][0])


{


biggestScore[howMany][0] = newScore;


biggestScore[howMany][1] = x;


biggestScore[howMany++][2] = y;


}


}


int random = rand() % howMany;


row = biggestScore[random][1];


column = biggestScore[random][2];


if(biggestScore[0][0] %26gt; 50)


{


board[row][column] = computer;


if(gameover(board, computer)) break;


else { game++; turn = 0; continue; }


}


else


{


board[row][column] = computer;


turn++;


}


} // End of if(choice %26gt; 0 ...





} // End of while(!quit) { ...


return 0;


} // End of main() { ...





void drawBoard(int board[][3])


{


char piece;


// Add a 'system("clear")' or 'system("cls")' if you want a clean screen


// I left this out becuase of portability between windows/unix.


// You may also create a simple for() loop that prints newlines for the same effect.


for (int i = 2; i %26gt; -1; i--)


{


for (int j = 0; j %26lt; 3; j++)


{


if(!board[i][j]) piece = (char) (i * 3 + j) + 49;


else if(board[i][j] == 2) piece = 'X';


else piece = 'O';





cout %26lt;%26lt; "| " %26lt;%26lt; piece %26lt;%26lt; " ";


}


cout %26lt;%26lt; "|\n";


}


} // End of drawBoard() ...





int getScore(int board[][3],int *x,int *y)


{


int score;


// The scores that a square will be given if the proper conditions are met:


// Modify these to see the results but remember that a score of over 50


// assumes a win; so be careful, or change that number in above code.


// Also remember that when the computer is getting these scores, it is


// assuming that it has not placed the piece yet on that square.


int scores[9] = {/*No Pieces on line*/ 0,


/*Undefined*/ 0,


/*One human piece*/ 4,


/*One computer piece*/ 3,


/*Two human pices*/ 8,


/*One of each*/ 2,


/*Two computer OR three human*/ 50,


/*Two human and one computer */ 1,


/*Two computer and one human */ 1 };


// Get horizontal score:


score = scores[(board[*x][0] + board[*x][1] + board[*x][2])];


// Get vertical score:


score += scores[(board[0][*y] + board[1][*y] + board[2][*y])];


// Get diagonal score (if possible)


if(*x == *y) // Check for '/' diagonal


score += scores[(board[0][0] + board[1][1] + board[2][2])];


if(*x + *y == 2) // Check for '\' diagonal


score += scores[(board[2][0] + board[1][1] + board[0][2])];





return score;


} // End of getScore() ...





int gameover(int board[][3], int player)


{


char again;


char name[3][9] = { "Computer", "Nobody", "You" };


drawBoard(board);


cout %26lt;%26lt; name[player % 3] %26lt;%26lt; " Won\nPlay again? (Y/N) ";


cin %26gt;%26gt; again;


if(again == 'Y' || again == 'y')


{


//Reset board


for (int i = 0; i %26lt; 3; i++)


for (int j = 0; j %26lt; 3; j++)


board[i][j] = 0;


return 0;


}


else


return 1;


}


Sunday, August 2, 2009

I need to create a C++ program which will analyze a sentence written in the code and print the longest words?

Say the sentence was "There's the spoons pass me one" I would like the program to print both "There's" as its the longest word with an " ' " and "spoons" because its the longest word.





I am trying to work it out using strings and arrays.





Im using Visuall Studio 2005 c++.





Also how can I get the program to tell me how many of 1 letter is in the whole sentence.

I need to create a C++ program which will analyze a sentence written in the code and print the longest words?
Here is a gift.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;vector%26gt;





using namespace std;





int findLetter(const string%26amp; str, const char letter)


{


size_t pos = 0;


int count = 0;





do


{


pos = str.find(letter, pos);


if (pos != string::npos)


{


count++;


pos++;


}





} while (pos != string::npos);





return(count);


}








void parseWords(vector%26lt;string%26gt; %26amp;a, string str)


{


size_t pos = 0;


size_t start = 0;


string tempStr;





do


{


pos = str.find(' ', start);


if (pos != string::npos)


{


tempStr = str.substr(start, pos - start);


pos++;


start = pos;


}


else


{


if (start != string::npos)


{


tempStr = str.substr(start);


}


}





a.push_back(tempStr);





} while (pos != string::npos);





}














int main(int argc, char *argv[])


{


const string sentence("There's the spoons pass me one");





char letter;





//find requested letter





cout %26lt;%26lt; "Enter letter to find in sentence: ";


cin %26gt;%26gt; letter;


cout %26lt;%26lt; letter %26lt;%26lt; " occurs: " %26lt;%26lt; findLetter(sentence, letter) %26lt;%26lt; " times." %26lt;%26lt; endl;





//load array of words in sentence





vector%26lt;string%26gt; array;





parseWords(array, sentence);





//find longest word





int largestSize = 0;


string largestStr;





vector%26lt;string%26gt;::iterator pos = array.begin();


vector%26lt;string%26gt;::iterator end = array.end();





for (; pos != end; ++pos)


{


if (pos-%26gt;length() %26gt; largestSize)


{


largestSize = pos-%26gt;length();


largestStr = *pos;


}


}





cout %26lt;%26lt; "Largest word was \"" %26lt;%26lt; largestStr %26lt;%26lt; "\" with a size of " %26lt;%26lt; largestSize %26lt;%26lt; endl;


}
Reply:the one letter of each is easy store the string in an array and go through it letter by letter until you reach the end, have a switch set up example





get the letter put in x





switch (x)





case (a) :





case (b) :








for the first part count letters until white space, store location thats longest somewhere, index through whole sentense if longer replace if not stay the same at the end have a pointer pointing to the beginning of the longest word and print it out or whatever








should get you started think correctly