General

Chat

Help me with C Assignment?

[b]Title is supposed to say "C++"[/b]

So I'm pretty new to C++ and I'm not really the best at using loops. Would anyone like to guide me through getting this output?

[header]Assignment Output[/header]
Write a program that asks the user how many numbers will be entered and then has the user enter those numbers. When this is done, report to the user the position of the first 7 entered and the last 7 entered. By position we mean, for example, that if the first 7 is the 2nd number entered then its position would be 2. Turn in the following 3 outputs exactly as you see them below to demonstrate that your program works in each case.

Sample screen output 1:

How many numbers will be entered? 8
Enter num: 5
Enter num: 7
Enter num: 6
Enter num: 7
Enter num: 7
Enter num: 3
Enter num: 8
Enter num: 6
The first 7 was in position 2
The last 7 was in position 5
Sample screen output 2:

How many numbers will be entered? 8
Enter num: 5
Enter num: 2
Enter num: 6
Enter num: 7
Enter num: 1
Enter num: 3
Enter num: 8
Enter num: 6
The first 7 was in position 4
The last 7 was in position 4
Sample screen output 3:

How many numbers will be entered? 8
Enter num: 5
Enter num: 1
Enter num: 6
Enter num: 5
Enter num: 9
Enter num: 3
Enter num: 8
Enter num: 6
Sorry, no sevens were entered.
Turn in your source code and by 3 outputs, one exactly like each of the three sample screen outputs given above.

________________________________________________________________________________________________
I believe I'm supposed to use a [b]for loop[/b]? Not entirely sure, but would anyone care to help me start off? Thank you very much! :][b][/b][b][/b][b][/b][b][/b]

October 6, 2013

11 Comments • Newest first

HolyDragon

[quote=AlaricFM]@HolyDragon: Ahh I see! LOL Thanks for explaining.

This is what I have so far:
#include <iostream>
using namespace std;

int main()
{ int count;
int numTimes;
int num;

cout << "How many numbers will be entered? : ";
cin >> numTimes;

for (count = 0; count < numTimes; count++){
cout << "Enter num:";
cin >> num;
}
system("Pause"
}

The only problem where I'm stuck at now is identifying the position of a specific number, (in this case 7)[/quote]

Back.

You need to readjust the function so it records to an array to store multiple numbers.
num[0], [1], etc.

For the next part, it's logic would be like
(Please fix this coding, I remember nothing about it)
The dashes are for spacing since basil doesn't allow empty spaces

i=0
Firstp=0
Secondp=0
while (i=/ numTimes-1)
-if (num[i] == 7)
--if (firstp ==0)
---firstp=i
--Secondp=i

if (firstp =/0)
cout firstp and secondp
else
-cout does not exist

Reply October 7, 2013 - edited
AlaricFM

@HolyDragon: Ahh I see! LOL Thanks for explaining.

This is what I have so far:
#include <iostream>
using namespace std;

int main()
{ int count;
int numTimes;
int num;

cout << "How many numbers will be entered? : ";
cin >> numTimes;

for (count = 0; count < numTimes; count++){
cout << "Enter num:";
cin >> num;
}
system("Pause"
}

The only problem where I'm stuck at now is identifying the position of a specific number, (in this case 7)

Reply October 7, 2013 - edited
HolyDragon

CharCount was a name. You can call it whatever you like. I named it CharCount out of habit from past assignments.

Reply October 6, 2013 - edited
AlaricFM

@JamesInNinja: Thank you very much! But could you explain what Array is? I believe we haven't gone that far in class. If you know about the latest Gaddis textbook for coding, I believe I'm as far as lesson 5.
[quote=HolyDragon]I haven't done programming in ages, but I'll help as much as I can.

Accept character number input
Assign this to variable. Let's call this CharCount

Create a array. I forgot how. Let's call it table for now.
For (i=0, i<CharCoun< i++) (Fix this)
Table(i)=Input

I think there was a find num function. Can't remember it. But you can use it to return the i in the table.[/quote]

I see, I think I understand what you're saying. I don't understand "char," though. Since it's dealing with numbers, shouldn't I declare the variable as an integer?

Reply October 6, 2013 - edited
HolyDragon

I haven't done programming in ages, but I'll help as much as I can.

Accept character number input
Assign this to variable. Let's call this CharCount

Create a array. I forgot how. Let's call it table for now.
For (i=0, i<CharCoun< i++) (Fix this)
Table(i)=Input

I think there was a find num function. Can't remember it. But you can use it to return the i in the table.

Reply October 6, 2013 - edited
JamesInNinja

[quote=AlaricFM]I believe I should make use of this format:

for(int x = 0; x < num; x++)

Right?[/quote]

Look at my first post now.

Reply October 6, 2013 - edited
AlaricFM

I believe I should make use of this format:

for(int x = 0; x < num; x++) /*Assuming the variables are already declared*/

Right?

Reply October 6, 2013 - edited
9Hades9

Possible without loop (recursive), but not as simple as loop.

Reply October 6, 2013 - edited
AlaricFM

@JamesInNinja: Thank you very much, I believe the introduction to both Cplusplus and Java are nearly identical. Pseudo code would be extremely useful!

[quote=tecul1]First off, how would you do it without a loop?[/quote]

I'm not sure actually..Is it even possible to do it without a loop? I feel absolutely clueless with this assignment. I'm not even sure how many variables I should declare and how to set everything up.

Is there a way I could write it so that when the user inputs a certain value, the output "Enter num:" will loop the same amount of the value of whatever the user typed in? (My apologies for failing to word that properly :X)

Reply October 6, 2013 - edited
JamesInNinja

I don't program C, but I do program a lot of Java so I'll give you pseudo code for it, this is a place holder I'll post in like 2 minutes

Array NewArray = new Array;
Ask the user to enter the number of #s they want, and store it in Variable X
for (int i; i < X; i++)
{ Get the user to enter number:
Array[x] = user input
}

//That will give get them to enter all the numbers.

As for finding the first and last, I'm too lazy to write code for it.

Reply October 6, 2013 - edited
tecul1

First off, how would you do it without a loop?

Reply October 6, 2013 - edited