#include <iostream>
int main()
{
int myArray[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
// loop down and across, show values at
// those positions.
for (int down = 0; down < 3; down++) {
// loop across
for (int across = 0; across < 3; across++) {
std::cout << myArray[down][across];
}
// do a new line ready for next loop
std::cout << std::endl;
}
// enter a value where 5 is.
std::cin >> myArray[1][1];
// now lets show the array again.
// loop down and across, show values at
// those positions.
for (int down = 0; down < 3; down++) {
// loop across
for (int across = 0; across < 3; across++) {
std::cout << myArray[down][across];
}
// do a new line ready for next loop
std::cout << std::endl;
}
return 0;
}
lets add a little twist lets say the declared array is a string type and i will display the numbers inside the quote "" lets say the numbers inside the quote is a integer
now what i want to know if is it possible to convert the string array to integer
can you please give me a example
Forget the arrays for a moment. Start simple:
Q: Is it possible to convert a string into an integer?
A: Yes, if the string contains only valid characters that can be interpreted as the representations of a value of the proper type [integer].
but lets say its a multidmensional array with a string data type and i want to convert a specific element in the array to a integer and then display the converted string together with the remaining elements is it possible?
More than likely not, simply because how is the program to know which element of the array is the one and only integer? Unless you want some sort of special algorithm, that is.
That is the reason for adopting a 2-d array with the [n][0]'s being students and the [n][1]'s being grades. This is the simple 'algorithm' to differentiate what stays as a string and what gets converted.
All this still boils down to the simple fact that a string array is an array of strings. ( Or of any single type. )
As kemort said you would need some algorithm written and have a fixed location in your string array for the integers. Would it not be easier for you to use a struct array/vector, or is this an assignment that requires you to use arrays?
If you were using a structure and declared like an array it would look something like this:
#include <iostream>
#include <string>
struct Record
{
std::string name;
int score;
};
int main()
{
constint NUM_RECORDS = 3;
Record Students[NUM_RECORDS];
// read in the 3 students
for (int i = 0; i < NUM_RECORDS; i++)
{
std::cout << "Enter name for student " << i + 1 << ": ";
std::cin >> Students[i].name;
std::cout << "What was their score: ";
std::cin >> Students[i].score;
}
// .... now do what you like with the data.
for (int i = 0; i < NUM_RECORDS; i++)
std::cout << Students[i].name << " scored " << Students[i].score << std::endl;
return 0;
}
Enter name for student 1: John
What was their score: 212
Enter name for student 2: Paul
What was their score: 958
Enter name for student 3: George
What was their score: 1003
John scored 212
Paul scored 958
George scored 1003
haha ok thanks i was just curious by the way can you please give sample practice exercises because i will be joining in a competition coverage are loops,function and arrays