I made a code wherein I should print the square and cube of the first 10 counting numbers but I used for loops. What I'm looking for is how to print the same output but using nested (for) statement.
Here is my code:
#include<stdio.h>
int main()
{
int x;
printf("x\tx*x\tx*x*x\t\n");
for(x=1; x<=10; x++)
printf("%d\t%d\t%d\n", x, x*x, x*x*x);
return 0;
}
With these few items to print, a nested for loop seems unecessary, but if you really want one, it could look like this:
for(x = 1; x <= 10; x++) {
for(int i = 0, X = x; i < 3; ++i, X *= x) {
printf("%d\t", X);
}
putchar('\n');
}
Demo
Here I have added the solution of this code along with its output using the nested for loop
I am currently trying to print a pyramid of hashes for the Mario problem set (less comfortable), and they won't print. Would someone be able to look at my code and pinpoint where I am going wrong? Thank you so much in advance.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int hashes;
int space;
do
{
int height = get_int("height: ");
}
while (height < 0 || height > 5);
{
for (int i = 0; i < height; i++)
{
int hashes = i;
for (hashes = (i + 1); hashes >= height; hashes++)
{
printf("#");
}
}
}
}
Assume you are using CS50 Lab and make to compile. This program will not compile because of shadow variables (as mentioned in the comments by #Weather Vane), and other errors. Therefore, I assume the executable that's running (i.e. ./mario) was the last good compile of an older version of the source that did not print results.
The for loop in this code is an infinite loop. If you correct the compile errors, you will see nothing but #.
Recommend you delete the "old" compiled version (rm mario in the terminal) and then modify the code so 1) it compiles and 2) does not create an infinite loop.
Remember, you declare a variable including the type, as here: int height;. But when you use the variable (as here int height = get_int("height: ");) you do not use the type declaration.
try this code, hope it helps
#include<stdio.h>
#include<cs50.h>
int main(void){
int height ;
do
{printf("height: ");
height = get_int();
}
while(height<0 || height>23);
for(int i=1;i<height+1;i++)
{
for(int j=0;j<height-i;j++)
{
printf("%s"," ");
}
for(int l=0;l<1;l++)
{
printf("#");
}
for(int k=0;k<i;k++)
{
printf("#");
}
printf("\n");
}
}
Very simple question. Why is the first letter cutting off? Prints "harles" going down. I see that I can hard code a fix with x = -1 but defeats the purpose of truly understanding what the underlying issue is and knowing solutions. Thanks.
#include <stdio.h>
int main()
{
int x = 0;
char iArray[7] = 'Charles';
while (x < 7) {
x++;
printf("%c\n", iArray[x]);
}
return 0;
}
You are first incrementating the index, then using it to print.
x++;
printf("%c\n", iArray[x]);
Changing to
printf("%c\n", iArray[x]);
x++;
will fix that problem and also avoid UB for accessing beyond the array.
The second mentioned problem occurs when the loop condition is still true for x==6 and the index is then incremented to 7, which accesses iArray[7]. That is beyond the array because the highest legally accessable index in an array of size 7 is index 6.
Try this!
#include <stdio.h>
int main()
{
int x = 0;
char iArray[7] = 'Charles';
while (x < 7) {
printf("%c\n", iArray[x]);
x++;//increment after printing
}
return 0;
}
You have increment x by 1 beforing using it, so x[0] will never print, x[0] contains 'C'. Increment the index 'x' after using it.
#include <stdio.h>
int main()
{
int x = 0;
char iArray[7] = "Charles";
while (x < 7) {
printf("%c\n", iArray[x]);
x++;
}
return 0;
}
I am attempting to process a two-dimensional array in C. I tried two nested for loops, but it seems that the two loops execute separately. I expect that the inside loop loops eight times for each loop of the outside loop, resulting in eight times the number of outside loops being the total number of loops.
As a simplified test, I tried this:
#include <stdio.h>
int main() {
int x = 0;
int y = 0;
for (; x < 7; x++, printf("(%d,%d)", x, y)) {
for (; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
This resulted in these results:
(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(1,8)(2,8)(3,8)(4,8)(5,8)(6,8)
Could somebody please explain to me why this might be happening? Thanks.
The reason is simple: you do not reinitialize the y variable in inner loop: when it reaches 8, it stays this way and the inner loop does not execute any more. Change your code to:
#include <stdio.h>
int main() {
for (x=0; x < 7; x++, printf("(%d,%d)", x, y)) {
for (y = 0; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
this is the part of my code I'm having trouble with. I can't understand why its doing it wrong. I have an array where it stores numbers 0 - 25 which are cases. The numbers are to be randomized and overwritten into the array. Only condition is is that no number can be doulbes, there can only be one of that number. I'm not asking you to do my code but do hint me or point me in the write directions. I am trying to learn :)
The problem lies within the second do loop. I can get the numbers to be randomized, but I get doubles. I have created a loop to check and fix this, but it's not working. The code does run, and doubles do still happen and I can't see why. It looks correct to me. Please look, thank you (:
This is what I have done originally (at the very end is where I am at now):
int check_double = 0;
int i = 0;
int counter = 0;
int array_adder = 0;
int random_number = 0;
int cases[] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
};
float money[] = {
0.01,1,5,10,25,50,75,100,200,300,400,500,750,1000,5000,10000,25000,50000,750000,100000,200000,300000,400000,500000,750000,1000000
};
//Randomize all case number and realine them in the array
srand ( time(NULL) );
do
{
cases[counter]= rand() % 26;
counter += 1;
printf("%d\n", cases[counter]);
}
while (counter <= 25);
//make sure there are no doubles in the array, just 0 - 25 and not a single number repeated twice
do
{
check_double = 0;
for (i = 0; i < counter; i++)
{
if (cases[counter] == cases[i])
{
cases[counter] = rand()% 26;
check_double == 1;
}
}
}
while (check_double != 0);
Currently, what I had achived after that was combing both loops and check for doubles as the array goes. This is what I made, it still has doubles and im not sure why, I only posted the cose with both loops combined:
do
{
cases[counter]= rand() % 26;
if (cases[counter]>=1);
for(i=0;i<=counter;i++)
if (cases[counter]==cases[i])
{
cases[counter]=rand()% 26;
}
printf("%d\n",cases[counter]);
counter+=1;
}
Robsta, you could try the following piece of code, I have run this in Dev-C++, any changes that you require can be made from your side. But, I assure you that this code generates what you intend.
int check_double = 0;
int i = 0;
int counter = 0;
int cases[] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
};
//Randomize all case number and realine them in the array
srand ( time(NULL) );
do
{
cases[counter]= rand() % 26;
for(i=0;i<counter;i++)
if (cases[counter]==cases[i]){
while (cases[counter]==cases[i])
{
cases[counter]=rand()% 26;
}
i=0;
}
printf("%d\t%d\n",counter,cases[counter]);
counter+=1;
}while (counter <= 25);
If you have any clarifications required, I would love to discuss with you.
-Sandip
You're only ever writing over the last value in the array:
for(i=0;i<counter;i++)
if (cases[counter]==cases[i])
You need to loop through as you are, then have an inner loop, where you compare all the other entries to the current one.
Even easier would be to do the loop where you set each random number, so when you set cases[3] for example, loop from 0 to 2 and check to see if your new value for 3 clashes, if so, wash - rinse - repeat!
You have this line of code:
check_double==1;
That doesn't change check_double because it's ==, not =. == compares; it doesn't assign. Change that line to this:
check_double=1;
A helpful compiler (clang in this example) will give you a warning about this:
test.c:5:14: warning: expression result unused [-Wunused-value]
check_double==1;
~~~~~~~~~~~~^ ~
You can't check for duplicates with a single loop. You need to at least compare every possible pair of elements to be able to see if there's a duplicate. I'm guessing you forgot to loop over counter somewhere inside the second do...while?
Note that your method is not guaranteed to terminate. (Very, very likely but not certain.) Why don't you simply shuffle the cases array? Shuffling is simple but tricky; see Fisher-Yates (or Knuth) Shuffle for a simple algorithm.
If you are asking how to randomly sequence the number 1-25 then you could do something like this. This is a very brute-force way of generating the sequence, but it does work and might give you a starting point for something more optimized.
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <conio.h>
const int LastNumber = 25;
bool HasEmpty(int available[LastNumber][2])
{
bool result = false;
for(int i = 0; i < LastNumber; i++)
{
if (available[i][1] == 0)
{
result = true;
break;
}
}
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
int available[LastNumber][2];
int newSequence[LastNumber];
srand((unsigned int)time(NULL));
for(int i = 0; i < LastNumber; i++)
{
available[i][0]=i;
available[i][1]=0;
}
int usedIndex = 0;
while (HasEmpty(available))
{
int temp = rand() % (LastNumber + 1);
if (available[temp][1] == 0)
{
newSequence[usedIndex++] = available[temp][0];
available[temp][1] = 1;
}
}
for(int i = 0; i < LastNumber; i++)
{
printf("%d\n",newSequence[i]);
}
getch();
return 0;
}