My program should ask user how many numbers he wanna input in the array , and than input one by one. Any number that's higher than 99 should be replaced by '0'.
That part works fine. But in the end created array should be placed in the table which needs to be in format (5 columns / depending rows).
This is what I wrote:
#include <stdio.h>
#define MAX 80
int main()
{
int n = 0;
int i,j;
int field[MAX]={0};
printf("how many numbers do u want to input? ");
scanf("%d",&n);
for ( i = 0; i < n; i++)
{
printf("Input number %d: ",i+1);
scanf("%d",&field[i]);
if(field[i] / 100 >= 1 )
{
field[i] = 0;
}
}
for ( i = 0; i<n; i++)
{
printf("\nfield[%d] = %d\n", i, field[i]);
}
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
return 0;
}
This part of your code can not be output in five columns
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
So if you change this, it will be executed
for(i=0; i <n;i+=5)
{
for(j =i; j <i+5;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
I hope this code will be help.
if i am wrong, please tell me and give a chance.
I want to help you and be recognized.
Related
What is wrong with this lottery Input - Raffle code?
I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2 sets of 7 numbers (mine and the random one) are compared, and the program outputs how much of my numbers are the same with the randomley picked ones. (the order of numbers doesn't matter) If I input more than one same numbers, for expample, 1 1 2 3 4 5 6, or input a number larger than 45, an error message must be printed. If both of these errors are true, there should be a seperate error message.
What is wrong with this lottery Input - Raffle code?
I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2
When I run the program, the radom picking of numbers (raffle) seems to be working fine, no overlapping numbers. the counting of matching number works as well. what is weird is the error messages. since the counting works, I assume the input values are well saved, but the program always outputs multiple lines of (7, exactly) "You cannot choose same number" . can anyone help me with this problem? The source code is below.
#include <stdio.h>
#include <stdlib.h>
int main () {
int yours[7];
printf("Buy yours: ");
for (int i=0; i<7; i++){
scanf("%d", &yours[i]);
for (int j=0; j<7; j++){
for (int k=0; k<7; k++){
if (yours[j] == yours[k] && yours[k]>45){
printf("You cannot choose same number and number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
}
else if (yours[j]>45){
printf("You cannot choose number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
break;
}
else if (yours[j] == yours[k]){
printf("You cannot choose same number.");
printf("\n\n");
printf("Buy yours: ");
break;
}
break;
}
break;
}
}
printf("Lottery result: ");
int lottery[7];
for (int i=0; i<7; i++){
lottery[i] = rand() %45 + 1;
for (int j=0; j<i; j++){
if (lottery[i] == lottery[j]) {
i--;
break;
}
}
}
for (int k=0; k<7; k++){
printf("%d ", lottery[k]);
}
int a = 0;
for (int i=0; i<7; i++){
for (int j=0; j<7; j++){
if (lottery[i] == yours[j]) {
a++;
}
}
}
printf("\n");
printf("The number of yours : %d", a);
return 0;
}
As other have already said, the code is a mess, since there's no identation, so it's quite difficult to understand and there's a high risk of making errors in the code.
Check out here: https://codehs.gitbooks.io/introcs/content/Programming-with-Karel/how-to-indent-your-code.html
Did you use something like the old version of Dev C++ to write the code? I've seen others struggling with that.
I would also suggest you to not specify array size as a number: e.g int array[7]; but to define a constant int this way:
#define array_size 7
int array[array_size];
This will let you to modify your program without the need to rewrite all the code and also will prevent you to have some bugs, and you should give self describing names to your variables.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int yours[7];
printf("Buy yours: ");
for (int i=0; i<7; i++)
{
scanf("%d", &yours[i]);
I think that the problem is here, since you are comparing values that still don't exist, the second for cycle and the third one are executed before the array yours if fully initialized. To fix it you have to change their conditions
for (int j=0; j<7; j++)
{
for (int k=0; k<7; k++)
{
if (yours[j] == yours[k] && yours[k]>45)
{
printf("You cannot choose same number and number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
}
else if (yours[j]>45)
{
printf("You cannot choose number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
break;
}
else if (yours[j] == yours[k])
{
printf("You cannot choose same number.");
printf("\n\n");
printf("Buy yours: ");
break;
}
break;
}
break;
}
}
printf("Lottery result: ");
int lottery[7];
for (int i=0; i<7; i++)
{
lottery[i] = rand() %45 + 1;
for (int j=0; j<i; j++)
{
if (lottery[i] == lottery[j])
{
i--;
break;
}
}
}
for (int k=0; k<7; k++)
{
printf("%d ", lottery[k]);
}
int a = 0; //What does it mean?
for (int i=0; i<7; i++) //Also check these
{
for (int j=0; j<7; j++)
{
if (lottery[i] == yours[j])
{
a++;
}
}
}
printf("\n");
printf("The number of yours : %d", a);
return 0;
}
I want to create a program that can read scores from user and display the scores output in the form of stars. But it seems that my code keep getting an infinity loop. Can anyone tell me what is wrong with my looping.
#include <stdio.h>
int main() {
int i, k, j;
int score[5];
for(i = 1; i < 6; i++) {
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 1; k < 6; k++) {
printf("%d. ", k);
for (j = 1; j <= score[k]; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Array indices are 0 based in C. So for an array with 5 elements the indices are from 0-4 inclusive. You are using indices 1-5 which results in buffer overflow. Below is the corrected program with the for loops fixed up to use the right indices:
#include <stdio.h>
#define NUM_SCORES 5
int main()
{
int i, k, j;
int score[NUM_SCORES];
for(i = 0; i < NUM_SCORES ; i++)
{
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 0; k < NUM_SCORES ; k++){
printf("%d. ", k);
for(j = 1; j <= score[k]; j++){
printf("*");
}
printf("\n");
}
return 0;
}
You should also add a check to ensure scanf is able to successfully read each input.
Can someone help me to figure out why my code is unable to accurately find the duplicate of elements?
#include <stdio.h>
int main() {
int array[10];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("The duplicates are : %d ", count);
}
I'm a beginner at this language so any advice and suggestions to help me solve this exercise will be much appreciated.
First of all the first loop runs 10 times even if the user enters less numbers. You can fix that by doing:
for (int i = 0; scanf_s("%d", &array[i]) == 1 && i < 10; i++);
Then the logic of the other two loops is wrong. I initially got wrong what you meant. I thought you wanted to know how many times a number is duplicated. So I wrote the wrong program and then modified it for your purposes. Here is your program:
#include <stdio.h>
int main() {
int n[10];
int dupes[5], d = 0;
int flag = 1, omg;
for ( omg = 0; scanf("%d", &n[omg]) == 1 && omg < 10; omg++);
for (int i = 0; i < omg; i++) {
for (int j = i+1; j < 10; j++) {
if( n[i] == n[j] ) {
if( d > 0 ) {
for(int k = 0; k < d; k++) {
if( n[i] == dupes[k] ) {
flag = 0;
break;
}
}
}
if( flag ) {
dupes[d] = n[i];
++d;
break;
}
else {
flag = 1;
break;
}
} // end outer if
}
}
printf("There are %d numbers that have at least one dupe\n", d);
return 0;
}
I named a variable omg out of desperation, writing this program was a nightmare. (Because it came from the ashes of a previous program)
Your code correctly determines the number of duplicate entries in the array.
If instead you want to determine the number of duplicated values, you must modify the algorithm:
#include <stdio.h>
int main() {
int array[10] = { 0 };
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (array[i] == array[j]) {
if (i < j)
count++;
if (i != j)
break;
}
}
}
printf("There are %d duplicate values\n", count);
return 0;
}
I use a structure 'Number' which contains the number and its duplicate, then I fill the array and I put it in ascending order then I calculate the number of duplicate of each number and I fill in the strecture like this :
my code:
#include <stdio.h>
#define size 10
typedef struct Number
{
int number;
int duplicate;
}Number;
int main()
{
int array[size];
Number array2[size];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int temp=size;
int temppppp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temppppp=array[i];
array[i]=array[j];
array[j]=temppppp;
}
}
}
printf("\n\n");
for (int i = 0; i < size; i++)
{
printf("[%d]",array[i]);
}
printf("\n\n");
int i=0;
int j=0;
while(i<size)
{count=1;
while(i<(size-1)&&array[i]==array[i+1])
{
count++;
i++;
}
if(count>=2)
{
array2[j].number=array[i-1];
array2[j].duplicate=count;
j++;
}
i++;
}
int p=0;
while(p<j)
{
printf("\n[%d] has duplicated %d times !\n",array2[p].number,array2[p].duplicate);
p=p+1;
}
printf("\n\n");
printf("\nThere are %d duplicate values\n", j);
}
Currently stuck on creating a pyramid out of hashes (#'s) based on a number given by user input. The example for CS50 only describes how to create a square based on the number given.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int n = get_int("Number:\n");
if(n>0 && n<9)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("#");
}
printf("\n");
}
}
Expected result is to create a pyramid that is x amount of #'s wide and tall based on the input given by user.
Actual result is a square that is x amount of #'s wide and tall based on the input given by user.
You need a loop which prints spaces until the second loop's counter (j) is less than n-i. Please see below:
#include <stdio.h>
int main(void)
{
int n, i, j, k;
printf("Number: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n-i; j++)
{
printf(" ");
}
for (k =0; k <= i; k++)
{
printf("# ");
}
printf("\n");
}
} // end main function
If n=3,the output is
1*2*3
7*8*9
4*5*6
If n=5,the output is
1*2*3*4*5
11*12*13*14*15
21*22*23*24*25
16*17*18*19*20
6*7*8*9*10
CODE:
int i, j, a[50][50], k = 1, m = 0;
for (i = 0; i < n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = k;
k++;
}
printf("\n");
}
m = k;
for (i = 1; i <= n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = m;
m++;
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d", a[i][j]);
}
printf("\n");
}
I am not so good in c language, but i think it will help you.
please have a look, and you can do making some change if any syntax error occurs but logic is clear.
#include <stdio.h>
#include <math.h>
void printOutput(int n){
int k = ceil(n/2);
int m =1;
int j =1;
int l =k;
int i;
int b;
for(i=1;i<=n;i++){
for(b=m;b<=m+n;b++){
printf(b);
}
printf("\n");
if(i<k){
j= (2*j);
m =n*j+1;
} else{
int z = n-i-1;
m= n+1 +n*(2)*z;
l =z-2;
}
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
printOutput(input);
}
#include<stdio.h>
#include<conio.h>
int k=1;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n/2+1;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k+n;
}
k=k-3*n;
for(int i=1;i<=n/2;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k-(n/2+1)*n;
}
}
This is a rough sketch of what you should do, there are some minor flaws with it... However, the functionality is there. Next time, if you can't understand what algorithm the code calls for, I suggest you write this array out on a sheet of paper and follow each row. Notice where each row gets placed, you should start to come up with a way to do this (there are more ways than this one...) It may seem hard at first, but if you want to be in this field, you have to have the mindset for it. I agree with the others, this is NOT a homework site, rather a site to help build off the knowledge you know, so begin to actually try to write the program, and then submit it here if you're having trouble with it.
#include <stdio.h>
void loadNprint(int size, int a[][size]){
int i,j,count=1,down=size-1, up =0;
for(i=0; i<size; i++){
for(j=0;j<size; j++){
if((i%2) == 0)a[up][j] = count;
if((i%2) == 1)a[down][j]= count;
count++;
}
if((i%2) == 0)up++;//keeping track of the rows in ascending order
if((i%2) == 1)down--;//keeping track of rows in descending order
}
for(i=0; i<size; i++){
for(j=0; j<size; j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
int myarray[input][input];
loadNprint(input,myarray);
}
This program works perfect. But if you want make some changes..do it yourself.
Next time please try some coding yourself before asking. This will print a different pattern for even numbers.
#include<stdio.h>
#include<conio.h>
int n,beginnew,cpy;
int main()
{
printf("Please enter a value : ");
scanf("%d",&n);
//process
beginnew=n-n/2+1;//beginning of new pattern
cpy=n-1;
for(int i=1;i<n+1;i++)
{
if(i<beginnew)
{
for(int h=n-1;h>=0;h--)
printf("%d * ", (n*(2*i-1)-h) );
}
else
{
for(int h=n-1;h>=0;h--)
printf("%d * ",(n*(cpy)-h) );
cpy=cpy-2;
}
printf("\n");
}
getch();
return 0;
}
//this code print Diagonal Pattern if matrix is
1 2 3
4 5 6
7 8 9
output is :
1
4 2
7 5 3
8 6
9
import java.util.*;
class DiagonalPattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x[][];
int i,j,row,col,p,temp=1,last=0;
System.out.println("how many array wants to create and size of array");
row=sc.nextInt();
col=sc.nextInt();
x=new int[row][col];
System.out.println("Enter " +row*col+ " elements of array of array");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
x[i][j]=sc.nextInt();
last=j;
}
}
for(i=0;i<row;i++)
{
System.out.println("");
int k=i;
for(j=0;j<=i;j++,k--)
{
if(j==col)
{
break;
}
else
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
for(p=x.length;p>0;p--,temp++)
{
System.out.println("");
i=x.length-1;
int k=i;
for(j=temp;j<=last;j++,k--)
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
}