Hello ladies and gents,

I just made the next exercise of this book and although it works as it should, I was wondering if any of you guys could tell me whether or not it could be improved.
The exercise goes as follows:
- Define a table of the names of months of the year and the number of days in each month. Write out that table. Do this twice: once using an array of char for the names and an array for the number fo days and once using an array of structures, with each structure holding the name of a month and the number of days in it.

I also added the following, initialized the struct with the values of the two arrays days and months and see the sizeof both arrays and the size of the struct and struct array.

Here's the code:

#include <iostream>
#include <string>

struct Date
{
	std::string strMonth;
	int strDays;
};

int main()
{	
	Date dt[12];

	std::cout << "Using two arrays:\n";

	char month[][12] = { "January", "February", "March", "April", "May", "June", 
		"July", "August", "September", "October", "November", "December"};
	int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	for (size_t i = 0; i < 12; i++)
		std::cout << days[i] << ' ' << month[i] << '\n';

	std::cout << std::endl;

	std::cout << "Using an array of struct:\n";

	for (size_t i = 0; i < 12; i++)
	{
		dt[i].strMonth = month[i];
		dt[i].strDays = days[i];
	}

	for (size_t i = 0; i < 12; i++)
		std::cout << dt[i].strDays << ' ' << dt[i].strMonth << '\n';

	std::cout << std::endl;

	std::cout << "Sizes are:\n" << "Days: " << sizeof(days) << "\nMonths: " << sizeof(month)
		<< "\nStruct Date: " << sizeof(Date) << "\nArray of struct: " << sizeof(dt);		

	std::cin.get();

	return 0;
}

Looks like but I think atleast one for loop can be reduced...

Assign the values to the structures members in the for loop in which you display the result of your first attempt ie loop 1.

for( int i = 0; i < 12; ++i )
{
    std::cout << "From first method: " << days[i] << " " << month[i] ;
    dt[i].strMonth = month[i] ;
    dt[i].strDays = days[i] ;
}

Hope it helps.

> char month[][12] The 12 here is not for the number of months, it is for the length of the longest month name (which has only 9 chars anyway).
It also forces "May" to occupy 12 characters as well (not so memory efficient).

If you say char *month[] = { "January", ... }; Then you let the compiler do all the counting for you, and each month name occupies the minimum space necessary.

OH, thanks for that Salem, I really thought that the [12] WAS for the amount of months, DARN :confused:

I understand why the way of using

char *month[]

is not only easier, but much faster aswell.

Hi guys,

Well, the next exercise goes as follows:

- Run some tests to see if your compiler really generates equivalent code for iteration using pointers and iteration using indexing. If different degrees of optimization can be requested, see if and how that affects the quality of the generated code.

What Ive got is this:

#include <iostream>

int main()
{	
	char mySentence[] = {"Hello World!"};

	for(size_t i = 0; mySentence[i] != 0; i++)
		std::cout << mySentence[i];
	std::cout << '\n';

	for(char *p = mySentence; *p != 0; p++)
		std::cout << *p;
	std::cout << '\n';

	std::cin.get();

	return 0;
}

I don't have a problem with generating the code, what I have a problem with is how to check whether they both generate equivalent code?

- I read that both should generate identical code using a modern compiler and that programmers choose between these two for aesthetical or logical grounds, just how can I check this to be true concerning the identical code?

- Concerning this part of the question: "If different degrees of optimization can be requested, see if and how that affects the quality of the generated code." How do I do that ?

I'm using VCEE and my OS is WinXP Pro if you need to know.

Thanks.

This is for VC6

-OUTPUT FILES-

/Fa[file] name assembly listing file     /Fo<file> name object file
/FA[sc] configure assembly listing       /Fp<file> name precompiled header file
/Fd[file] name .PDB file                 /Fr[file] name source browser file
/Fe<file> name executable file           /FR[file] name extended .SBR file
/Fm[file] name map file

Somewhere in the IDE, you can configure the compiler output files to include the output of the generated assembler file.

Or for small test programs, just use the command line with something like
cl /Fafoo.asm foo.cpp

Well, thanks for that, guess I'll have to find out how to change those IDE settings in the Project -> properties -> Configuration properties -> General -> Output Directory?

If you don't mind Salem, could you help me out with this exercise aswell.

It goes like this:

- Find an example where it would make sense to use a name in its own initializer.

I'm not sure what to make out of this, is this related to something like this:

#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string myString = "String";
	
	cin.get();

         return 0;
}

Really don't understand what the point of this exercise is ?

This article has been dead for over six months. Start a new discussion instead.