So I have two arrays, a[17] and b[12]. I want to compare the first 12 numbers of each, and if the numbers match have it print out a "0", if they dont match have it print out a "1". But it doesnt work. It should print "000001111111 " but it doesnt. Can anyone please tell me why?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i, j;
int a[17] = {1,0,1,0,0,1,0,1,0,1,0,0,0,1,1,0,1};
int b[12] = {1,0,1,0,0,0,1,0,1,0,1,1};
for(i=0;i<12;i++)
for(j=0;j<12;j++)
if(a[i] == b[j])
printf("1");
else
printf("0");
system("pause");
return 0;
}
Your code should be:
for(i=0;i<12;i++) {
if(a[i] == b[i]) {
printf("1");
} else {
printf("0");
}
}
There is no need of two loops.
You want to compare elements from arrays at same indices, so the indices i should be same for both arrays.
Related
#include <stdio.h>
int printArray(a[], unsigned int n){
if (n == 4){
printf("%d", a[4]);
}
else {
printf("%d", printArray(a,n+1)); // here is the error, I know. But why?
}
}
int main(){
int a[5] = {1,2,3,4,5};
printArray(a,0);
}
I am a begginer with C. I am trying to print each of the elements of the array, but it only prints the last one correctly, and not the former ones. I know there's something about recursive way wrong. It's the first time I use recursive way without return and using printf. Maybe is this that misleaded me?
Output:
$ ./a.out
5
0
1
1
0
You should print the array item, not the return value of the function, Use this code
int printArray(int *a, unsigned int n){
if (n == 4){
printf("%d", a[4]);
}
else {
printf("%d", a[n]);
printArray(a,n+1);
}
}
I am new to C programming and I want to display how many times did "ma" showed up for example "mamama" there is 3 "ma" but in my code when I write "mamam" it displays 3 "ma". Sorry for my bad explanation... I recently started studying C programming.
I tried with do-while but I end up screwing even more..
#include <string.h>
#include <stdio.h>
int main()
{
char a[81];
int i, j;
int zbroj=0;
i=0;
fgets(a,81,stdin);
for(i=0; i<'m';i++)
{
for(j=0; j<'a';j++)
{
while(a[i] !='\0')
{
if(a[i] == 'm' || a[j]=='a')
{
zbroj++;
}
i++;
}
}
}
printf("%d\n", zbroj);
return 0;
}
Well, the end goal is when I type "mamam" program should write down there is 2 "ma".
This is what you need:
int main()
{
char a[81];
int i, j;
int zbroj=0;
i=0;
fgets(a,81,stdin);
while(a[i] !='\0') {
if(a[i] == 'm' && a[i+1]=='a')
{
zbroj++;
i++;
}
i++;
};
printf("%d\n", zbroj);
return 0;
}
Your first iteration are not needed. You need to iterate though the string and check if you have found the letter you want and then check the next one. Then increase the counter. Try to understand what is that each line of code does though. You will need it.
hint I have also changed || to &&
This is a code that has to take an input array from the user and input the same after removing the duplicates. However, I am unsure on how to incorporate an input array in this, and right now it has the elements hardcoded. This is my first week of programming so I apologize if this is a silly question. This is the code:
#include <stdio.h>
#include <stdbool.h>
#define nelems 8
int main()
{
int l[nelems] = {1,2,3,1,4,4,5,6};
for(int m=0;m<nelems;m++)
{
bool wase = 0;
for(int n=0;n<nelems && m>n;n++)
{
if (l[m] == l[n] && m != n)
wase = 1;
}
if (wase == 0){
printf("%d\n", l[m]);
}
}
return 0;
}
Try using a for loop and scanf.
int i;
for(i=0;i<nelems;i++){
scanf("%d",&l[i]);
}
This is what you need.
#include <stdio.h>
#include <stdbool.h>
#define nelems 8
int main()
{
int i;
int l[nelems] ;
for(i=0;i<nelems;i++)
{
printf("enter %d number :",i);
scanf("%d",&l[i]);
}
for(int m=0;m<nelems;m++)
{
bool wase = 0;
for(int n=0;n<nelems && m>n;n++)
{
if (l[m] == l[n] && m != n)
wase = 1;
}
if (wase == 0){
printf("%d\n", l[m]);
}
}
return 0;
}
If you like int-type array, you can just declare another one:
int input[nelems];
and follow the user968000 advice, remembering that when you are typing the sequence in your console you have to put a white space between each number.
To avoid that, I'd rather use char-type arrays, declared as follows:
char l[nelems] = {'1', '2', '3' /*etc.*/};
char input[nelems];
Then you make a for loop, as user968000 suggested:
int i;
for(i=0;i<nelems;i++)
scanf("%c", &input[i]);
In this case you won't need the white spaces between the digits. Notice the '&' character in the scanf function: just put it as I showed, you'll surely learn what it is in next lessons.
So you have an input array and you can handle it as you want.
I tried to evaluate a postfix expression using only digits so after understanding the concept and implented the code, which I thought it was right, it didn't generate a correct answers but
after I made a few changes it worked but here I want to know what is the mistake I did in the first so I cannot repeat it in the future !
These are the two codes (first one wrong and second one right )
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
int b[100]={0};
int x=0,j=0,i=0;
scanf("%s",s);
for (i=0; i<strlen(s); i++)
{
if(s[i]>=48 && s[i]<=57)
{
b[j]=s[i];
j++;
}
else
{
if (s[i]=='+')
{
x=(b[j-1]-48) + (b[j-2]-48);
b[j-2]=x;
j--;
}
else if (s[i]=='-')
{
x=(b[j-2]-48) - (b[j-1]-48);
b[j-2]=x;
j--;
}
else if (s[i]=='*')
{
x=(b[j-1]-48) * (b[j-2]-48);
b[j-2]=x;
j--;
}
else
{
x=(b[j-2]-48) / (b[j-1]-48);
b[j-2]=x;
j--;
}
}
}
printf("%d",b[0]);
return 0;
}
The correct code
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
int b[100]={0};
int x=0,j=0,i=0;
scanf("%s",s);
for (i=0; i<strlen(s); i++)
{
if(s[i]>=48 && s[i]<=57)
{
b[j]=s[i]-48; // modified
j++;
}
else
{
if (s[i]=='+')
{
x=(b[j-1]) + (b[j-2]); //modified
b[j-2]=x;
j--;
}
else if (s[i]=='-')
{
x=(b[j-2]) - (b[j-1]); // modofied
b[j-2]=x;
j--;
}
else if (s[i]=='*')
{
x=(b[j-1]) * (b[j-2]); //modified
b[j-2]=x;
j--;
}
else
{
x=(b[j-2]) / (b[j-1]); // modified
b[j-2]=x;
j--;
}
}
}
printf("%d",b[0]);
return 0;
}
in order to test the program we need to enter an expression in reverse polish notation like 32*1+ which is in infix notation is 3*2+1 so we will get a correct answer 7
Someone who doesn't know how the evaluation of Reverse Polish Notation works may not figure out the problem. Your code is correct but the algorithm is wrong.
I will take 32*1+ as an example :
For the first two numbers your code is correct but after calculating the result and pushing it to the stack, troubles begin.
When it encounters an operator the next time it will subtract 48 from each of the two numbers extracted from the stack and your result is a product of that rule, which is wrong.
Okay so I have been working on a small rouge like game to teach myself c however I cannot figure out why the array's end up changing randomly after initialization. Here is the code I have:
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
const int x=10;
const int y=10;
int rexit[]={5,5};
int player[]={1,1};
int enemy[]={x,y};
int useless[]={1,1};
/*
int getinput(int len){
char temp[100];
return(atoi(strtok(fgets(temp,len+1,stdin),"\n")));
}
*/
void bad(){
float bob;
float temp[8]={1,1};
temp[1]=player[1]-enemy[1];
temp[2]=player[2]-enemy[2];
bob=pow(temp[1],2)+pow(temp[2],2);
printf("%f\n",bob);
if (sqrt(bob)<=5){
if (abs(player[1]-enemy[1])>abs(player[2]-enemy[2])){
if (player[1]-enemy[1]<0 && enemy[1]-1>=0){
enemy[1]=enemy[1]-1;
}
else if (enemy[1]+1<=x && player[1]-enemy[1]!=0){
enemy[1]=enemy[1]+1;
}
}
else;
if (player[2]-enemy[2]<0 && enemy[2]-1>=0){
enemy[2]=enemy[2]-1;
}
else if (enemy[2]+1<=y && enemy[2]-player[2]!=0){
enemy[2]=enemy[2]+1;
}
}
}
void map() {
int s;
int a;
bad();
printf("%i %i\n",rexit[1],rexit[2]);
printf("+");
for (a=0;a<=x;a++){
printf("-");
}
printf("+\n");
for (s=0;s<=y;s++){
printf("|");
for (a=0;a<=x;a++){
if ((player[1]==rexit[1] && player[2]==rexit[2]) || (player[1]==enemy[1] & player[2]==enemy[2])){
exit(EXIT_SUCCESS);
}
else if (rexit[1]==s && rexit[2]==a){
printf("E");
}
else if (player[1]==s && player[2]==a){
printf("*");
}
else if (enemy[1]==s && enemy[2]==a){
printf("#");
}
else{
printf(".");
}
}
printf("|\n");
}
printf("+");
for (a=0;a<=x;a++){
printf("-");
}
printf("+\n");
}
void move(){
char me=_getch();
int temp=0;
me=toupper(me);
if (me=='W'){ player[1]=player[1]-1; if (player[1]<=0) player[1]=0;}
else if (me=='S'){ player[1]=player[1]+1; if (player[1]>=y) player[1]=y;}
else if (me=='A'){ player[2]=player[2]-1; if (player[2]<=0) player[2]=0;}
else if (me=='D'){ player[2]=player[2]+1; if (player[2]>=x) player[2]=x;}
else {temp=1;}
if (temp==1){
move();}
else{
system("cls");
map();}
}
void circle(char c,int x)
{
int i,j;
for(i=-x;i<x;i++)
{
for(j=-x;j<x;j++)
{
if(i*i+j*j<x*x)
printf("%c",c);
else
printf(" ");
}
printf("\n");
}
}
void main(){
printf("%i %i\n",enemy[1],enemy[2]);
printf("%i %i\n",useless[1],useless[2]);
system("title BioGames");
system("color A"); // the colours are from 1 to 15
map();
while (true){
move();
}
}
You are treating your array indexing as 1-based. Arrays in C are 0-based.
For example, you declare:
int player[]={1,1};
The only valid indices for this array are 0 and 1 (ie player[0] and player[1]).
There is no such thing as player[2] - that is accessing memory outside the array bounds. This is probably why you think your arrays are changing randomly. They are either being affected by out-of-bound writes to other arrays, or you are simply experiencing undefined behaviour.
Your arrays useless and enemy are declared to contain two values each at initialization. Your main is printing array indices 1 and 2. In C, however, array indices start at 0. So you should be printing indices 0 and 1.