I have to make a scrabble scoring game using 2 arrays. The first array holds the user inputted word and the second array holds the value of each letter. Lastly a function is needed to calculate the score. Im having trouble assigning the user values to the second array to get the score and getting the right code for the function.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
int scoreCalculator(int total);
char userWord;
int points;
int total=0;
int main()
{
char userWord[11];
for (int i=0; i<11; i++)
{
userWord[i]='\0';
}
cout<<"Enter your word less than 10 letters: "<<endl;
cin>>userWord;
cout<<"Here is the word you inputted: "<<userWord<<endl;
int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
for(int j=0; j<26; j++)
{
userWord[i]
}
cout<<"Here is your score: "<<scoreCalculator(total)<<endl;
}
int scoreCalculator(int total)
{
total+=scrabblePoints[j];
}
This is what I have so far and where im stuck at
#include <iostream>
int main()
{
std::string input;
std::cin>>input;
// fill this with scrable values
int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int ans = 0;
for(int i=0;i<input.size();i++)
{
ans += scrable_values[input[i]-97];
}
return ans;
}
Related
I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
int array[100];
int sum;
for(int i=0; i<100; i++){
cout<<"Insert element "<<i<<": ";
cin>>array[i];
sum = array[i]+ //summ with the next array input;
cout<<sum;
}
return 0;
}
means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input
C++ standard library already has a function to do this which is std::accumulate:
#include <iostream>
#include <numeric>
int main() {
int array[5] = {1,2,3,4,5};
int total = std::accumulate(std::begin(array), std::end(array), 0);
return 0;
}
If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.
initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum.
Hope this will help.
You can use a do while loop:
#include<iostream>
#include<string>
#include<math.h>
int main()
{
int array[100];
int sum=0;
int i=0;
std::cout<<"Insert element"<<" "<<i<<": ";
std::cin>>array[i];
do
{
sum=sum+array[i];
std::cout<<sum<<std::endl;
i++;
std::cout<<"Insert element"<<" "<<i<<": ";
}while(std::cin>>array[i]);
return 0;
}
If all you want is the sum then just compute the sum. No need to store anything:
#include <iostream>
#include <string>
#include <math.h>
int main() {
int sum = 0;
for(int i=0; i<100; i++){
std::cout << "Insert element " << i << ": ";
int t;
std::cin >> t;
sum += t
std::cout << sum;
}
}
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct Students
{
char name[30];
char first_name[30];
int n1;
int n2;
int n3;
}student list[10];
int n;
void reading_student_list()
{
int i;
ifstream f("in.txt");
f>>n;
for(i=0;i<n;i++)
{
f>>student_list[i].name;
f>>student_list[i].first_name;
f>>student_list[i].n1;
f>>student_list[i].n2;
f>>student_list[i].n3;
}
f.close();
}
void showing_student_list()
{
int i;
ofstream g("out.txt");
g<<"\n Student list: ";
for(i=0;i<n;i++)
{
g<<"\n\n";
g<<student_list[i].name<<" ";
g<<student_list[i].first_name<<" ";
g<<student_list[i].n1<<" ";
g<<student_list[i].n2<<" ";
g<<student_list[i].n3<<" ";
g<<endl<<"Average: "<<float(student_list[i].n1+student_list[i].n2+student_list[i].n3)/3;
}
cout<<"\n Open file out.txt";
g.close();
}
int main()
{
reading_student_list();
showing_student_list();
}
I did the average for the grades per student, but I can't figure out how to find the average for the whole class.
After doing the average for the whole class, I can figure out the students that have their average below average of the class and the highest average of the class.
Here's how you would add up all the n1 values (whatever that means).
int total = 0; // start the total at zero
for (int i = 0; i < n; i++) // for each student
total += list[i].n1; // add list[i].n1 to total
Hopefully you can adapt that to suit your particular requirements.
The program will count the position where a specific string occurs within a character array and store that position within an array. As an example the string 'has' would be at positions [0,13,25,33] when compared to the string 'hassan is a hassler who hassles hasslers'. Two main character arrays are being used; str[] and sub[]. str being the string from where the occurrence is counted from and sub the string compared. I have attached my idea of the code. Any help is appreciated and as I am still a student and not a pro, I would very much appreciate constructive help rather than comments on how my code and workflow is sloppy. I wish to display the array with all the positions and the program does not display anything other than the return value, which for some reasons is mostly very big numbers.
#include <iostream>
#include <cstring>
using namespace std;
void allPositionsofSub()
{
int nstr;
int nsub;
char str[100]; //i initially wished to use the variable nstr but it wouldnt work with cin.get
char sub[nsub];
cout<<"Enter the string: ";
cin.get(str, 100);
cout<<"Enter the sub: ";
cin>>sub;
int num;
int count;
int count1=0;
int outdisplay[count];
for(int nstr1=0;nstr1<=99;nstr1++)
{
char n;
n = sub[nstr1];
while (nstr1<=nsub)
{
if (n==str[nstr1])
{
outdisplay[num]=nstr1;// this is where i think the problem perhaps lies.
num++;
count++;
}
}
}
while(count1<=count)
{
cout<<outdisplay[count1]<<", ";
count1++;
}
cout<<"-1";
}
int main()
{
allPositionsofSub();
}
updated code:
#include <iostream>
#include <cstring>
using namespace std;
void allPositionsofSub()
{
int nstr;
int nsub=20;
char str[100];
char sub[20];
cout<<"Enter the string: ";
cin.get(str, 100);
cout<<"Enter the sub: ";
cin>>sub;
int num;
int count;
int count1=0;
int outdisplay[count];
for(int nstr1=0;nstr1<=99;nstr1++)
{
char n;
n = sub[nstr1];
if (n==str[nstr1])
{
outdisplay[num]=nstr1;
num++;
count++;
}
}
while(count1<=count)
{
cout<<outdisplay[count1]<<", ";
count1++;
}
cout<<"-1";
}
int main()
{
allPositionsofSub();
}
So the basic idea is to enumerate over every position in the source string and check whether it is the start of your substring.
It is better for you to try implementing the following pseudo-code, and come back if you have any more questions.
# substr_len: The length of the substring you are looking for.
# str_len: The length of the source string.
# result: An array of int with sufficient length.
let match_count = 0
for (i in range 0..str_len-substr_len)
let match = true
for (j in range 0..substr_len-1)
if (substr[j] != str[i + j])
match = false
break
if (match)
result[match_count] = i
match_count = match_count + 1
I have to create a game of 5 rounds simulating a soccer shootout using a 2x3 array that represents the goal. The computer randomly picks 3 places to block and the user chooses one place to shoot. If the user chooses a coordinate that is not blocked then its a goal. Two functions are needed, one where the computer picks 3 random places to block and the other function is prints out the goal every round. If the user scores 3 times then they win, otherwise they lose.
The output should look like this(B=Blocked, G=Goal, "-" = empty space):
B - B
B - G
Ive been stuck on my code and have gotten an error that I just cant seem to fix within both functions
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[]);
void shot(char shooter[]);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}
You have to use correct types for arguments and have to match the prototype declaration and definition of functions.
This code compiles:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
void computerPick(char soccer[][3]);
void shot(char shooter[][3], int userInputY, int userInputX);
int main()
{
int userInputX;
int userInputY;
srand(time(NULL));
char soccer[2][3];
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
soccer[i][j]='-';
}
}
cout<<"Pick a X coordinate to shoot at: "<<endl;
cin>>userInputX;
cout<<"Pick a Y coordinate to shoot at: "<<endl;
cin>>userInputY;
computerPick(soccer);
shot(soccer,userInputY,userInputX);
}
void computerPick(char soccer[][3])
{
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
}
void shot(char shooter[][3], int userInputY, int userInputX)
{
int score=0;
if(shooter[userInputX][userInputY]!='B')
cout<<"shot is good"<<endl;
else
cout<<"shot is blocked"<<endl;
}
You might want to look at these parts again:
char soccer[2][3];
and
int x = rand()%3;
int y = rand()%2;
soccer[x][y]='B';
Also note that booleans would be clearer in your two dimensional array instead of chars of 'B' or 'G'.
Also when using multidimensional arrays as parameters, you can pass them as
int foo(int (*array)[5][10])
Which means that you are passing a pointer to an array of fixed size 5-10
I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.