Click here to Skip to main content
15,066,062 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>



/* Print an array of integers with a title. */


void print_int_array(int *int_array, int n, char *title) {
 printf("%s\n", title);
 for(int i = 0; i < n; i++) {
 printf(" %d\n", int_array[i]);
 }
}



/* Fill an array of integers beginning with the value first, ending with or under the value last,
 and stepping by step. The final element filled may have value last, but no higher. It is up
 to the calling function to make sure that the combination of first, last and step makes sense,
 and that there is enough space in the array. Returns the number of items in the array. */

     int squares[20];

     squares[0] = 0;
     squares[1] = 2;
     squares[2] = 4;
     squares[3] = 9;
     squares[4] = 16;
     squares[5] = 25;
     squares[6] = 36;
     squares[7] = 49;
     squares[8] = 64;
     squares[9] = 81;
     squares[10] = 100;
     squares[11] = 121;
     squares[12] = 144;
     squares[13] = 169;
     squares[14] = 196;
     squares[15] = 225;
     squares[16] = 256;
     squares[17] = 289;
     squares[18] = 324;
     squares[19] = 361;







int create_int_step_array(int *int_array, int first, int last, int step) {


  int i;

    for(i = 0; i < step; i++) {
        int_array[i] = i * i;
    }


}
/* Print the first ten squares of positive even numbers. */
void print_even_squares(void) {
 int squares[10];
 int n;
 n = create_int_step_array(squares, 2, 20, 2);
 for(int i = 0; i < n; i++) {
 squares[i] = squares[i] * squares[i];
 }
 print_int_array(squares, n, "First ten squares of positive even numbers");
}


/* Print the first twenty squares of positive odd numbers. */

void print_odd_squares(void) {

 }



/* Print the first ten cubes of positive integers. */
void print_cubes(void) {

 }



}
int main(void) {
 print_even_squares();
 print_odd_squares();
 print_cubes();
 return 0;
}


What I have tried:

I am a novice beginning to learn so go easy on me - with the comments in the program you can see what I am attempting to do. Major problem I am encountering is with the squares; It tells me 'conflicting types with squares.' I am not sure where to go from here. Thank you.
Posted
Updated 20-Feb-21 19:29pm
v2

The variable squares is defined at the global level. As such you cannot assign values to it in the global space as a series of assignment statements. You may, however assign a value at the same time as you define a global variable e.g.
C
int squares[20] = { 0, 1, 4, 9, 16, 25, 36, 49, 64
                   81, 100, 121, 144, 169, 197,
                   225, 246, 289, 324, 361 };


Probably a better idea, though is to assign the values in a loop inside main
C
int squares[20];
int main()
{
    for(int i = 0; i < 20; ++i)  /* assumes you are using at least C99 */
        squares[i] = i * i;
    /* rest of code */
}

Note that this also avoids data errors, like the one you have in your data. Every programmer learns that its usually better to have the computer do "donkey work" of filling in data like this than risking a brain "oops" or character transposition or other typo in hand typed data. We all know that the square of 16 is 256, but your computer doesn't know that your array squares should contain the square of the index at each element, so if you mistakenly typed 265, it's happy with that. Tracking that kind of error down can be very difficult - as the brain will often show you what you're expecting, so you can convince yourself that 265 is indeed the square of 16, so the program is generating bad results for an unknown reason.

I would also suggest that A) using global variables is, generally, a bad idea, and B) there's no reason to have the squares array in this case - all the data can be computed at run time. Unless you needed the values a lot (think millions, or even billions), its doubtful there's any real-time measurable time difference between computing the square of a number and getting a value from an array.
   
I will help you with one of them :
C++
int create_int_step_array( int *array, int first, int last, int step )
{
    int count = 0;
    int value;
    for( value = first; value <= last; value += step )
    {
        array[ count ] = value;
        ++count;
    }
    return count;
}
   
Quote:
It tells me 'conflicting types with squares.'

It is a good idea to give exact error message, including the line number. It helps to spot the error.

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Print an array of integers with a title. */
void print_int_array(int *int_array, int n, char *title) {
	printf("%s\n", title);
	for(int i = 0; i < n; i++) {
		printf(" %d\n", int_array[i]);
	}
}

/* Fill an array of integers beginning with the value first, ending with or under the value last,
and stepping by step. The final element filled may have value last, but no higher. It is up
to the calling function to make sure that the combination of first, last and step makes sense,
and that there is enough space in the array. Returns the number of items in the array. */

int squares[20];

squares[0] = 0;
squares[1] = 2;
squares[2] = 4;
squares[3] = 9;
squares[4] = 16;
squares[5] = 25;
squares[6] = 36;
squares[7] = 49;
squares[8] = 64;
squares[9] = 81;
squares[10] = 100;
squares[11] = 121;
squares[12] = 144;
squares[13] = 169;
squares[14] = 196;
squares[15] = 225;
squares[16] = 256;
squares[17] = 289;
squares[18] = 324;
squares[19] = 361;

int create_int_step_array(int *int_array, int first, int last, int step) {
	int i;
	for(i = 0; i < step; i++) {
		int_array[i] = i * i;
	}
}

/* Print the first ten squares of positive even numbers. */
void print_even_squares(void) {
	int squares[10];
	int n;
	n = create_int_step_array(squares, 2, 20, 2);
	for(int i = 0; i < n; i++) {
		squares[i] = squares[i] * squares[i];
	}
	print_int_array(squares, n, "First ten squares of positive even numbers");
}

/* Print the first twenty squares of positive odd numbers. */
void print_odd_squares(void) {

}

/* Print the first ten cubes of positive integers. */
void print_cubes(void) {

}

} // this is probably an error too

int main(void) {
	print_even_squares();
	print_odd_squares();
	print_cubes();
	return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900