11 Replies - 811 Views - Last Post: 04 March 2014 - 02:04 AM Rate Topic: -----

#1 Ph0x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-March 14

I have two question about arrays

Posted 02 March 2014 - 03:17 PM

Firstly, I'm not a free-rider. I've been studying for days, but I've gotten stuck on two questions. If you who is old hand can lead me, I'd glad for your help.
first question:
Write a program that asks the user to type 10 integers of an array. The program
will then display either "the array is growing", "the array is decreasing", "the
array is constant", or "the array is growing and decreasing."(Hint: compare
consecutive numbers in array and check whether it is increasing or not or
constant)

I wrote the program very well, but do I have to use "bool" like in c++ ? I haven't known how to code in c++ yet.So, I don't want to use "bool" in C.
my second question:
Posted Image
I also wrote code about it but it looks like backslash and one,two,three,four,five how can I do it like this ?
Again thanks for your helps

@edit: if I have grammar mistakes,excuse me my english

Is This A Good Question/Topic? 0
  • +

Replies To: I have two question about arrays

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: I have two question about arrays

Posted 02 March 2014 - 03:20 PM

Quote

I wrote the program very well, but do I have to use "bool" like in c++ ? I haven't known how to code in c++ yet.So, I don't want to use "bool" in C.

I probably wouldn't use a boolean value here. The hint your instructor gave is spot on. Look at consecutive elements. How do you know if an array is increasing? Wouldn't each of the elements be increasing?

For your second question, what do you notice about the numbers along the diagonal? What is the pattern? Think about a loop here.
Was This Post Helpful? 0
  • +
  • -

#3 Ph0x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-March 14

Re: I have two question about arrays

Posted 02 March 2014 - 03:33 PM

View Postmacosxnerd101, on 02 March 2014 - 03:20 PM, said:

Quote

I wrote the program very well, but do I have to use "bool" like in c++ ? I haven't known how to code in c++ yet.So, I don't want to use "bool" in C.

I probably wouldn't use a boolean value here. The hint your instructor gave is spot on. Look at consecutive elements. How do you know if an array is increasing? Wouldn't each of the elements be increasing?

For your second question, what do you notice about the numbers along the diagonal? What is the pattern? Think about a loop here.

I don't have any idea about first question :s
for second question:
#include <stdio.h>
#include <conio.h>


int main()
{
	int a[5][5] = {0}, i, j;
	for (i = 0; i < 5 ; i++)
	for (j = 0; j < 5; j++){

		a[i][j] = j + 1; // or j*j like these

		if (i == j)
			printf("%d", a[i][j]);
		else
			printf("%c", 32);
		if (j == 4)
			printf("\n");
		}

	_getch();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: I have two question about arrays

Posted 02 March 2014 - 03:36 PM

Let me ask you this- if I give you the following sequence of numbers, tell me which ones are increasing, decreasing, or both:
Sequence 1: 1, 2, 3, 4, 5
Sequence 2: 5, 4, 3, 2, 1
Sequence 3: 1, 2, 3, 2, 1

Also, a note. You should use iostream and cin/cout, and avoid getch() and conio.


For the second question, I doubt that will give you the correct output. Seriously- look at the pattern from the picture.
Was This Post Helpful? 0
  • +
  • -

#5 Ph0x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-March 14

Re: I have two question about arrays

Posted 02 March 2014 - 03:45 PM

View Postmacosxnerd101, on 02 March 2014 - 03:36 PM, said:

Let me ask you this- if I give you the following sequence of numbers, tell me which ones are increasing, decreasing, or both:
Sequence 1: 1, 2, 3, 4, 5
Sequence 2: 5, 4, 3, 2, 1
Sequence 3: 1, 2, 3, 2, 1

Also, a note. You should use iostream and cin/cout, and avoid getch() and conio.


For the second question, I doubt that will give you the correct output. Seriously- look at the pattern from the picture.

I'm trying for the second question.
my code for first question:
#include <stdio.h>
#include <conio.h>
#define N 5
#define true 1
#define false 0
typedef char bool;

int main()
{

		int a[N], i;
		bool found = false;
		bool up = false, down = false;
		printf("Please enter an integer: ");
		scanf_s("%d", &a[0]);

		for (i = 1; i<N; i++)
		{
			printf("Please enter an integer: ");
			scanf_s("%d", &a[i]);
			if (a[i - 1]>a[i]) down = true;
			if (a[i - 1]<a[i]) up = true;
		}

		printf("Array is: "); 
		if (up&&down)
			printf("Increasing and Decreasing");
		else if (up)
			printf("Increasing");
		else if (down)
			printf("Decreasing");
		else if (!(up&&down))
			printf("Constant");
	
	_getch();
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: I have two question about arrays

Posted 02 March 2014 - 03:47 PM

Per my previous post:

Quote

Also, a note. You should use iostream and cin/cout, and avoid getch() and conio.


Great- so you got the first question otherwise? (If you didn't get it, you need to ask specific questions about it, including posting a specific description of your problems and error messages exactly as they appear).
Was This Post Helpful? 0
  • +
  • -

#7 Ph0x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-March 14

Re: I have two question about arrays

Posted 02 March 2014 - 03:48 PM

do I have to use "bool"? I wonder its answer
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: I have two question about arrays

Posted 02 March 2014 - 03:50 PM

You don't *have* to use a bool. Think more about logic, and less about "do I have to use _ type of variable?" If you think about the latter, you're probably not really looking at the problem and the logic to solve it.

Also, I answered your question about the bool above:

Quote

I probably wouldn't use a boolean value here. The hint your instructor gave is spot on. Look at consecutive elements. How do you know if an array is increasing? Wouldn't each of the elements be increasing?

Was This Post Helpful? 0
  • +
  • -

#9 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: I have two question about arrays

Posted 02 March 2014 - 04:12 PM

You also, don't explain your problem with your second problem. Is it with displaying, calculating, showing wrong code doesn't help explain.
I can give some tips from guessing. The diagonal where i = j (I call the normal diagonal) is from top left to bottom right . Also if the cell depends on another cell it doesn't help to refer to itself.

a[i][j] = j + 1; Something like that makes the entire row the j+1; Which then you go on to not display. So your wasting execution. It should go it after the if. And all this does is display 1 2 3 4 5 down the normal diagonal.
Was This Post Helpful? 0
  • +
  • -

#10 Ph0x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 02-March 14

Re: I have two question about arrays

Posted 03 March 2014 - 02:50 PM

View Postmacosxnerd101, on 02 March 2014 - 03:36 PM, said:

Let me ask you this- if I give you the following sequence of numbers, tell me which ones are increasing, decreasing, or both:
Sequence 1: 1, 2, 3, 4, 5
Sequence 2: 5, 4, 3, 2, 1
Sequence 3: 1, 2, 3, 2, 1

Also, a note. You should use iostream and cin/cout, and avoid getch() and conio.


For the second question, I doubt that will give you the correct output. Seriously- look at the pattern from the picture.

#include <stdio.h>


int main()
{
		//04 13 22 31 40
	int a[5][5] = { 0 }, i, j, p[5] = {16,8,4,2,1};
	for (i = 0; i < 5 ; i++)
	for (j = 0; j < 5; j++){
		a[i][j] = 5*p[j]; // or j*j like these

		if (i+j==4)
			printf("%d", a[i][j]);
		else
			printf("%c", 32);
		if (j == 4)
			printf("\n");
		}

	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: I have two question about arrays

Posted 03 March 2014 - 02:51 PM

If you have a question, please ask it and please be specific. Nobody here will try to decipher your question based on a code dump.
Was This Post Helpful? 0
  • +
  • -

#12 click_here   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 300
  • Joined: 25-November 13

Re: I have two question about arrays

Posted 04 March 2014 - 02:04 AM

system(pause);


This won't work on all systems.

The traditional way of stopping the program before it closes is to use the standard function
getchar();


Where the user hits enter to finish the program.

It looks like you want to make the program in C, not C++, so you are correct in not using cin/cout.

You should start your main function using "void" in the arguments, so that it is consistant with the C99 standard

i.e.
int main(void)
{
  ...




I don't like that you do not use indentation and curly brackets for the first "for" loop. Although it is not a problem here, it was the cause of a BIG security mistake by Apple with SSL varification (research "gotofail bug"). You'll want to get out of that habbit ASAP.

As for your program (based on how "macosxnerd101" explained it), have you learnt about "enum" yet? You could use something like: "undecided" (for entering the loop), "rising" (if second number is higher than first), "falling", and "constant".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1