14 Replies - 781 Views - Last Post: 24 March 2012 - 02:21 PM Rate Topic: -----

#1 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Arrays I think?

Posted 24 March 2012 - 12:46 PM

Hi, I am moving along my programming course rather well but I have been given this task;

1.	Write a program that :
•	Initialises an array with the values 3, 5, 7, -1
•	Prints out the array, not including the -1 (in other words the -1 is used to terminate the array)
2.	Write a program that:
•	Declares an int array of size 100
•	Prompts the user for the number of values to be entered into the array and reads in the user’s response (which must be less than 99!)
•	Uses the value that the user entered to control a loop which reads in that number of numbers, putting each in the next array location
•	Uses the value that the user entered to control a loop to print the values back out from the array
3.	Modify the program from task 2 so that when the user has entered all of the numbers, the next free location has -1 put in it. The program should then print out all of the numbers using -1 as the array terminator (i.e. what the loop tests for so that it knows when to stop printing)
4.	BONUS TASK:  Modify the program in task 3 so that it reads in characters rather than numbers. Use the control character ‘\0’ as a terminator rather than -1.


And I have managed task 1, but my question is for task two. This is what I have so far in my mind as to what needs to be done -;

I will need to declay an array of size 100 so "int na[100];" but as it wants the user to be able to input the size of the array I would also need to declare something like this "int ArraySize;" or is that wrong already? then from what I have done so far, I could use a "while" loop such as "while(counter<ArraySize) { scanf("%d",na[(this is where I get confused since I need to read in more than one input and store it in a different location in the array but I don't know how to do this which is part of my question)];

then a printf to display the results I guess ?

Basically I'm not asking anyone to do this for me, rather to tell me the topics I need to research in order to complete this task as I am not sure what to search . I've come across things like 2d and multidimensional arrays? but I am not sure what to read so if someone that understands the task could lend me some time I'd appreciate it guys .

Sorry for the wall of text

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays I think?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Arrays I think?

Posted 24 March 2012 - 12:54 PM

Well, your mention of scanf, and the use of int na[100]; has me thinking you don't want C#, where you posted this, but C/C++. So I'll move this there.

And you might want to read the link on Arrays in my signature.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Arrays I think?

Posted 24 March 2012 - 12:55 PM

I'm not trying to sound like a smart ass...
But how would you not know what material is used for this project? Didn't you just cover it in the course? Wouldn't it be the current chapter in the course textbook?

I mean if you have to go research it and learn it yourself then why are you in a course? A "Learn C# in 3 weeks" book would probably guide you better than this course sounds like it is doing.

Quote

but as it wants the user to be able to input the size of the array I would also need to declare something like this "int ArraySize;" or is that wrong already?


Get the user input value, then init the array to that size
int[] someArray;
int bob = GetUserInput();
someArray = new Array[bob];


I suspect you have the materials you need to read already: Course textbook. More likely you just need to stop, plan THEN code.


Reminder to new folks anxious to help by just throwing out working code: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

90% or more of the problems we see here on homework assignments is because students don't stop and plan nor block out their code into logical segments.
Read this article to get started on the right foot:
This is the 'Help me with my homework' article you wanted.


Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

These articles may also apply:
Stages of asking for homework help on a forum.
I don't want you to write my code, just give me ideas on how to solve my problem. - Read this article
How NOT to go about your assignment
"It doesn't work" isn't good enough - How you can help us, to help you

What have to you tried to code so far? - Click the link

What part of what you have tried isn't working?
Are you getting error messages, or is it just not giving you the results you wanted?

(In other words, you at least have to make and show us your good faith effort to do your own coding. We are here to help you, not do it for you.)
What does this error mean?
Was This Post Helpful? 1
  • +
  • -

#4 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 12:56 PM

View PostJackOfAllTrades, on 24 March 2012 - 12:54 PM, said:

Well, your mention of scanf, and the use of int na[100]; has me thinking you don't want C#, where you posted this, but C/C++. So I'll move this there.

And you might want to read the link on Arrays in my signature.

Oh you are right, sorry for that
Was This Post Helpful? 0
  • +
  • -

#5 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 01:05 PM

View PosttlhIn`toq, on 24 March 2012 - 12:55 PM, said:

I'm not trying to sound like a smart ass...
But how would you not know what material is used for this project? Didn't you just cover it in the course? Wouldn't it be the current chapter in the course textbook?

I mean if you have to go research it and learn it yourself then why are you in a course? A "Learn C# in 3 weeks" book would probably guide you better than this course sounds like it is doing.

Quote

but as it wants the user to be able to input the size of the array I would also need to declare something like this "int ArraySize;" or is that wrong already?


Get the user input value, then init the array to that size
int[] someArray;
int bob = GetUserInput();
someArray = new Array[bob];


I suspect you have the materials you need to read already: Course textbook. More likely you just need to stop, plan THEN code.


Reminder to new folks anxious to help by just throwing out working code: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

90% or more of the problems we see here on homework assignments is because students don't stop and plan nor block out their code into logical segments.
Read this article to get started on the right foot:
This is the 'Help me with my homework' article you wanted.


Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

// Try #1 - May 1, 0900hrs
//    code
//    code  
//    code
// Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
//    code
//    code  
//    code
// Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
    code
    code  
    code


If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

These articles may also apply:
Stages of asking for homework help on a forum.
I don't want you to write my code, just give me ideas on how to solve my problem. - Read this article
How NOT to go about your assignment
"It doesn't work" isn't good enough - How you can help us, to help you

What have to you tried to code so far? - Click the link

What part of what you have tried isn't working?
Are you getting error messages, or is it just not giving you the results you wanted?

(In other words, you at least have to make and show us your good faith effort to do your own coding. We are here to help you, not do it for you.)
What does this error mean?

I see what you are saying, but it literally is the worse run course I have ever partaken in. Its the first year of Software engineering actually, but for this unit we turn up, get a sheet that has the information I posted above and thats it. But what you have shown me already is more than I was taught here thanks. Also I get what you are saying about use the users input it init the array , but how do I generate for e.g. if the user wants 10 fields, I would use the scanf("%d",&na[0]); scanf("%d",&na[1]); scanf("%d",&na[2]); scanf("%d",&na[3]); right? but how do I "auto" generate these so that it only asked for the amount of &na[]'s needed. Sorry if this makes absolutely no sense at all.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Arrays I think?

Posted 24 March 2012 - 01:08 PM

You don't have a book for this class? All you would need is too know any of the different loops you can use (I would suggest a for-loop for this) and the number of times you need to loop ("iterate"). A few other parts of your homework will need loops as well so you might want to read up on them if you don't know them.

This post has been edited by vividexstance: 24 March 2012 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 01:12 PM

View Postvividexstance, on 24 March 2012 - 01:08 PM, said:

You don't have a book for this class? All you would need is too know any of the different loops you can use (I would suggest a for-loop for this) and the number of times you need to loop ("iterate"). A few other parts of your homework will need loops as well so you might want to read up on them if you don't know them.


I realize I am coming across as really stupid, but I do understand loops fairly well. My main problem with this task is getting my head around storing each user input in a new array 'na [0,1,2]' (<-- what would you call that by the way)after the user has set the size.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Arrays I think?

Posted 24 March 2012 - 01:16 PM

View PostDatvid, on 24 March 2012 - 02:05 PM, said:

I see what you are saying, but it literally is the worse run course I have ever partaken in.

See FAQ # 26. (Click the SHOW button below)
No really. We hear this complaint THAT much.

FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler







View PostDatvid, on 24 March 2012 - 02:05 PM, said:

But what you have shown me already is more than I was taught here thanks. Also I get what you are saying about use the users input it init the array , but how do I generate for e.g. if the user wants 10 fields, I would use the
scanf("%d",&na[0]);  
scanf("%d",&na[1]);   
scanf("%d",&na[2]);   
scanf("%d",&na[3]);
right? but how do I "auto" generate these so that it only asked for the amount of &na[]'s needed. Sorry if this makes absolutely no sense at all.


Use a loop.
Was This Post Helpful? 1
  • +
  • -

#9 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 01:28 PM

Ok this is the last thing I'll ask.

If i did something like this

the user wants 10 inputs, and I had 'int counter; counter=0;' and the for the loop it would be while(counter<0) {scanf("%d",&na[counter]); Counter++;} so that while the counter is < 0 it will keep adding one to the counter field until it reached the user inputted number?

This post has been edited by macosxnerd101: 24 March 2012 - 01:54 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Arrays I think?

Posted 24 March 2012 - 01:37 PM

You may want to consider a for() loop instead. It is really designed for what you are thinking of doing. I think you exit condition is incorrect. That loop should only run once, the first time you run the loop when you increment your counter the loop will stop.

Jim
Was This Post Helpful? 3
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Arrays I think?

Posted 24 March 2012 - 01:38 PM

TIP:
You don't have to quote the entire previous message every time. It just makes the entire thread very, very long. If your comments are directly related to the last post you can just use reply box at the end of the page.

If you are responding to just a small portion of an earlier comment, then use the Quote and Respond button, but edit the portion quoted to just the part you are responding to. It keeps the reading more coherent to the rest of us without bloating the threads.




Re-worked so we can read the code:
:code:

View PostDatvid, on 24 March 2012 - 02:28 PM, said:

If i did something like this

the user wants 10 inputs, and I had
int counter;  counter=0;
// and the for the loop it would be 

while(counter<0) 
{
   scanf("%d",&na[counter]); 
   Counter++;
}


so that while the counter is < 0 it will keep adding one to the counter field until it reached the user inputted number?


Can someone who does C++ look that over? It's not jelling in my C# brain today.
Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Arrays I think?

Posted 24 March 2012 - 02:11 PM

Quote

int counter;
counter=0;

while(counter<0) 
{
   scanf("%d",&na[counter]); 
   Counter++;
}


That loop would never get entered. Like jimblumberg said, the OP should use a for-loop because that is what they were designed for.
Was This Post Helpful? 0
  • +
  • -

#13 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 02:16 PM

This was my code for the first part of the work sheet

int main(int argc, char *argv[])
{
	char c;
	int na[4] = {3,5,7,-1};

	for(int i = 0; na[i] != -1; i++)
	{
		printf("%d\n",na[i]);
	}

	c=getchar();
}




I should be using a for loop like that one then I guess?
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Arrays I think?

Posted 24 March 2012 - 02:17 PM

That was the last part (Part 3) of your homework. The first part said to get the number of elements to input and then use that number in the loop's conditional instead of the -1.
Was This Post Helpful? 0
  • +
  • -

#15 Datvid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-March 12

Re: Arrays I think?

Posted 24 March 2012 - 02:21 PM

View Postvividexstance, on 24 March 2012 - 02:17 PM, said:

That was the last part (Part 3) of your homework. The first part said to get the number of elements to input and then use that number in the loop's conditional instead of the -1.

No that was for the second part of question 1, using the -1 as a terminator. But I feel I need to point out this isn't 'homework' this is a 'do it if you want' unit as in its not graded (I'm a 21 year old university student, not that it changes anything I just don't want people to think they are helping a child cheat on their homework or something).

I think I can see how the for loop would be better suited for it actually, I'm going to try and write up something and I'll keep you updated on whether I figure this out for myself.

Thanks guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1